CCManageCryptoCloud.m 12 KB

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