CCManageCryptoCloud.m 12 KB

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