CCManageCryptoCloud.m 12 KB

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