CCManageCryptoCloud.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. //
  2. // CCManageCryptoCloud.m
  3. // Crypto Cloud Technology Nextcloud
  4. //
  5. // Created by Marino Faggiana on 13/02/17.
  6. // Copyright (c) 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 "CCManageCryptoCloud.h"
  24. #import "AppDelegate.h"
  25. #import "NCBridgeSwift.h"
  26. @implementation CCManageCryptoCloud
  27. -(id)init
  28. {
  29. XLFormDescriptor *form ;
  30. XLFormSectionDescriptor *section;
  31. XLFormRowDescriptor *row;
  32. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTheming) name:@"changeTheming" object:nil];
  33. form = [XLFormDescriptor formDescriptorWithTitle:NSLocalizedString(@"_crypto_cloud_system_", nil)];
  34. section = [XLFormSectionDescriptor formSection];
  35. [form addFormSection:section];
  36. section.footerTitle = NSLocalizedString(@"_footer_crypto_cloud_", nil);
  37. // Activation Crypto Cloud Mode
  38. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"activatecryptocloud" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"_activation_crypto_cloud_", nil)];
  39. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  40. [row.cellConfig setObject:[UIImage imageNamed:@"settingsCryptoCloud"] forKey:@"imageView.image"];
  41. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  42. [row.cellConfig setObject:[NCBrandColor sharedInstance].brand forKey:@"textLabel.textColor"];
  43. row.action.formSelector = @selector(activateCryptoCloud:);
  44. row.hidden = @(YES);
  45. [section addFormRow:row];
  46. // Deactivation Crypto Cloud Mode
  47. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"deactivatecryptocloud" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"_deactivation_crypto_cloud_", nil)];
  48. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  49. [row.cellConfig setObject:[UIImage imageNamed:@"settingsRemoveCryptoCloud"] forKey:@"imageView.image"];
  50. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  51. [row.cellConfig setObject:[NCBrandColor sharedInstance].brand forKey:@"textLabel.textColor"];
  52. row.action.formSelector = @selector(disactivateCryptoCloud:);
  53. row.hidden = @(YES);
  54. [section addFormRow:row];
  55. section = [XLFormSectionDescriptor formSection];
  56. [form addFormSection:section];
  57. return [super initWithForm:form];
  58. }
  59. // Apparirà
  60. - (void)viewWillAppear:(BOOL)animated
  61. {
  62. [super viewWillAppear:animated];
  63. self.tableView.backgroundColor = [NCBrandColor sharedInstance].tableBackground;
  64. // Color
  65. [app aspectNavigationControllerBar:self.navigationController.navigationBar encrypted:NO online:[app.reachability isReachable] hidden:NO];
  66. [app aspectTabBar:self.tabBarController.tabBar hidden:NO];
  67. [self reloadForm];
  68. }
  69. - (void)changeTheming
  70. {
  71. if (self.isViewLoaded && self.view.window)
  72. [app changeTheming:self];
  73. }
  74. - (void)activateCryptoCloud:(XLFormRowDescriptor *)sender
  75. {
  76. [self deselectFormRow:sender];
  77. CCBKPasscode *viewController = [[CCBKPasscode alloc] initWithNibName:nil bundle:nil];
  78. viewController.delegate = self;
  79. viewController.type = BKPasscodeViewControllerNewPasscodeType;
  80. viewController.passcodeStyle = BKPasscodeInputViewNormalPasscodeStyle;
  81. viewController.passcodeInputView.maximumLength = 64;
  82. viewController.title = NSLocalizedString(@"_key_aes_256_", nil);
  83. viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(passcodeViewCloseButtonPressed:)];
  84. viewController.navigationItem.leftBarButtonItem.tintColor = [NCBrandColor sharedInstance].cryptocloud;
  85. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
  86. [self presentViewController:navigationController animated:YES completion:nil];
  87. }
  88. - (void)disactivateCryptoCloud:(XLFormRowDescriptor *)sender
  89. {
  90. CCBKPasscode *viewController = [[CCBKPasscode alloc] initWithNibName:nil bundle:nil];
  91. viewController.delegate = self;
  92. viewController.type = BKPasscodeViewControllerCheckPasscodeType;
  93. viewController.passcodeStyle = BKPasscodeInputViewNormalPasscodeStyle;
  94. viewController.passcodeInputView.maximumLength = 64;
  95. viewController.title = NSLocalizedString(@"_check_key_aes_256_", nil);
  96. viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(passcodeViewCloseButtonPressed:)];
  97. viewController.navigationItem.leftBarButtonItem.tintColor = [NCBrandColor sharedInstance].cryptocloud;
  98. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
  99. [self presentViewController:navigationController animated:YES completion:nil];
  100. }
  101. - (void)closeCryptoCloudSecurity
  102. {
  103. // @"Attivazione avvenuta correttamente, ora potrai usufruire di tutte le funzionalità aggiuntive. Ti ricordiamo che i file cifrati possono essere decifrati sono sui dispositivi iOS"
  104. UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"_info_", nil) message:NSLocalizedString(@"_alert_activation_crypto_cloud_", nil) delegate:nil cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(@"_ok_", nil), nil];
  105. [alertView show];
  106. }
  107. - (void)activateSecurityOptions
  108. {
  109. CCManageCryptoCloudSecurity *vc = [[CCManageCryptoCloudSecurity alloc] initWithDelegate:self];
  110. UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:vc];
  111. [nc setModalPresentationStyle:UIModalPresentationFormSheet];
  112. [self presentViewController:nc animated:YES completion:nil];
  113. }
  114. #pragma --------------------------------------------------------------------------------------------
  115. #pragma mark == BKPasscodeViewController ==
  116. #pragma --------------------------------------------------------------------------------------------
  117. - (void)passcodeViewController:(BKPasscodeViewController *)aViewController didFinishWithPasscode:(NSString *)aPasscode
  118. {
  119. switch (aViewController.type) {
  120. case BKPasscodeViewControllerNewPasscodeType: {
  121. // min passcode 4 chars
  122. if ([aPasscode length] >= 4) {
  123. [CCUtility setKeyChainPasscodeForUUID:[CCUtility getUUID] conPasscode:aPasscode];
  124. // verify
  125. NSString *pwd = [CCUtility getKeyChainPasscodeForUUID:[CCUtility getUUID]];
  126. if ([pwd isEqualToString:aPasscode] == NO || pwd == nil) {
  127. UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"_error_", nil) message:@"Fatal error writing key" delegate:nil cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(@"_ok_", nil), nil];
  128. [alertView show];
  129. } else {
  130. // Crypto Cloud Mode : Activated
  131. app.isCryptoCloudMode = YES;
  132. // force reload all directory for all users
  133. [CCCoreData clearAllDateReadDirectory];
  134. // 3D Touch
  135. [app configDynamicShortcutItems];
  136. // Request : Send Passcode email
  137. [self performSelector:@selector(activateSecurityOptions) withObject:nil afterDelay:0.1];
  138. }
  139. } else {
  140. UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"_error_", nil) message:NSLocalizedString(@"_passcode_too_short_", nil) delegate:nil cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(@"_ok_", nil), nil];
  141. [alertView show];
  142. }
  143. [aViewController dismissViewControllerAnimated:YES completion:nil];
  144. }
  145. break;
  146. case BKPasscodeViewControllerCheckPasscodeType: {
  147. // Crypto Cloud Mode : Deactivated
  148. [CCUtility adminRemovePasscode];
  149. app.isCryptoCloudMode = NO;
  150. // 3D touch
  151. [app configDynamicShortcutItems];
  152. // force reload all directory for all users and all metadata cryptated
  153. [CCCoreData clearAllDateReadDirectory];
  154. [CCCoreData deleteMetadataWithPredicate:[NSPredicate predicateWithFormat:@"(cryptated == 1)"]];
  155. UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"_info_", nil) message:NSLocalizedString(@"_alert_deactivation_crypto_cloud_", nil) delegate:nil cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(@"_ok_", nil), nil];
  156. [alertView show];
  157. [aViewController dismissViewControllerAnimated:YES completion:nil];
  158. }
  159. break;
  160. default:
  161. break;
  162. }
  163. }
  164. - (void)passcodeViewController:(BKPasscodeViewController *)aViewController authenticatePasscode:(NSString *)aPasscode resultHandler:(void (^)(BOOL))aResultHandler
  165. {
  166. if (aViewController.type == BKPasscodeViewControllerCheckPasscodeType) {
  167. NSString *key = [CCUtility getKeyChainPasscodeForUUID:[CCUtility getUUID]];
  168. if ([aPasscode isEqualToString:key]) {
  169. self.lockUntilDate = nil;
  170. self.failedAttempts = 0;
  171. aResultHandler(YES);
  172. } else {
  173. aResultHandler(NO);
  174. }
  175. }
  176. }
  177. - (void)passcodeViewControllerDidFailAttempt:(BKPasscodeViewController *)aViewController
  178. {
  179. self.failedAttempts++;
  180. if (self.failedAttempts > 5) {
  181. NSTimeInterval timeInterval = 60;
  182. if (self.failedAttempts > 6) {
  183. NSUInteger multiplier = self.failedAttempts - 6;
  184. timeInterval = (5 * 60) * multiplier;
  185. if (timeInterval > 3600 * 24) {
  186. timeInterval = 3600 * 24;
  187. }
  188. }
  189. self.lockUntilDate = [NSDate dateWithTimeIntervalSinceNow:timeInterval];
  190. }
  191. }
  192. - (NSUInteger)passcodeViewControllerNumberOfFailedAttempts:(BKPasscodeViewController *)aViewController
  193. {
  194. return self.failedAttempts;
  195. }
  196. - (NSDate *)passcodeViewControllerLockUntilDate:(BKPasscodeViewController *)aViewController
  197. {
  198. return self.lockUntilDate;
  199. }
  200. - (void)passcodeViewCloseButtonPressed:(id)sender
  201. {
  202. [self dismissViewControllerAnimated:YES completion:nil];
  203. }
  204. #pragma --------------------------------------------------------------------------------------------
  205. #pragma mark === Reload Form ===
  206. #pragma --------------------------------------------------------------------------------------------
  207. - (void)reloadForm
  208. {
  209. XLFormRowDescriptor *rowActivateCryptoCloud = [self.form formRowWithTag:@"activatecryptocloud"];
  210. XLFormRowDescriptor *rowDeactivateCryptoCloud = [self.form formRowWithTag:@"deactivatecryptocloud"];
  211. XLFormRowDescriptor *rowSendMailEncryptPass = [self.form formRowWithTag:@"sendmailencryptpass"];
  212. if (app.isCryptoCloudMode) {
  213. rowActivateCryptoCloud.hidden = @(YES);
  214. rowDeactivateCryptoCloud.hidden = @(NO);
  215. rowSendMailEncryptPass.hidden = @(NO);
  216. } else {
  217. rowActivateCryptoCloud.hidden = @(NO);
  218. rowDeactivateCryptoCloud.hidden = @(YES);
  219. rowSendMailEncryptPass.hidden = @(YES);
  220. }
  221. [self.tableView reloadData];
  222. }
  223. @end