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