CCManageCryptoCloud.m 12 KB

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