CCManageCryptoCloud.m 12 KB

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