CCManageCryptoCloud.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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", nil)];
  40. section = [XLFormSectionDescriptor formSection];
  41. [form addFormSection:section];
  42. // Activation Crypto Cloud Mode
  43. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"activatecryptocloud" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"_activation_crypto_cloud_", nil)];
  44. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  45. [row.cellConfig setObject:[UIImage imageNamed:image_settingsCryptoCloud] forKey:@"imageView.image"];
  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.action.formSelector = @selector(disactivateCryptoCloud:);
  54. row.hidden = @(YES);
  55. [section addFormRow:row];
  56. section = [XLFormSectionDescriptor formSection];
  57. [form addFormSection:section];
  58. self.form = form;
  59. }
  60. - (void)viewDidLoad
  61. {
  62. [super viewDidLoad];
  63. // Color
  64. [CCAspect aspectNavigationControllerBar:self.navigationController.navigationBar hidden:NO];
  65. [CCAspect aspectTabBar:self.tabBarController.tabBar hidden:NO];
  66. }
  67. // Apparirà
  68. - (void)viewWillAppear:(BOOL)animated
  69. {
  70. [super viewWillAppear:animated];
  71. // Color
  72. [CCAspect aspectNavigationControllerBar:self.navigationController.navigationBar hidden:NO];
  73. [CCAspect aspectTabBar:self.tabBarController.tabBar hidden:NO];
  74. [self reloadForm];
  75. }
  76. - (void)activateCryptoCloud:(XLFormRowDescriptor *)sender
  77. {
  78. [self deselectFormRow:sender];
  79. CCBKPasscode *viewController = [[CCBKPasscode alloc] initWithNibName:nil bundle:nil];
  80. viewController.delegate = self;
  81. viewController.type = BKPasscodeViewControllerNewPasscodeType;
  82. viewController.passcodeStyle = BKPasscodeInputViewNormalPasscodeStyle;
  83. viewController.passcodeInputView.maximumLength = 64;
  84. viewController.title = NSLocalizedString(@"_key_aes_256_", nil);
  85. viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(passcodeViewCloseButtonPressed:)];
  86. viewController.navigationItem.leftBarButtonItem.tintColor = COLOR_ENCRYPTED;
  87. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
  88. [self presentViewController:navigationController animated:YES completion:nil];
  89. }
  90. - (void)disactivateCryptoCloud:(XLFormRowDescriptor *)sender
  91. {
  92. CCBKPasscode *viewController = [[CCBKPasscode alloc] initWithNibName:nil bundle:nil];
  93. viewController.delegate = self;
  94. viewController.type = BKPasscodeViewControllerCheckPasscodeType;
  95. viewController.passcodeStyle = BKPasscodeInputViewNormalPasscodeStyle;
  96. viewController.passcodeInputView.maximumLength = 64;
  97. viewController.title = NSLocalizedString(@"_check_key_aes_256_", nil);
  98. viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(passcodeViewCloseButtonPressed:)];
  99. viewController.navigationItem.leftBarButtonItem.tintColor = COLOR_ENCRYPTED;
  100. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
  101. [self presentViewController:navigationController animated:YES completion:nil];
  102. }
  103. - (void)checkEncryptPass:(XLFormRowDescriptor *)sender
  104. {
  105. }
  106. - (void)closeSecurityOptions
  107. {
  108. UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"_OK_", nil) message:@"Attivazione avvenuta correttamente, ora potrai usufruire di tutte le funzionalità aggiuntive. Ti ricordiamo che i file cifrati possono essere decifrati sono sui dispositivi iOS" delegate:nil cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(@"_ok_", nil), nil];
  109. [alertView show];
  110. }
  111. - (void)activateSecurityOptions
  112. {
  113. CCSecurityOptions *securityOptionsVC = [[CCSecurityOptions alloc] initWithDelegate:self];
  114. UINavigationController *securityOptionsNC = [[UINavigationController alloc] initWithRootViewController:securityOptionsVC];
  115. [securityOptionsNC setModalPresentationStyle:UIModalPresentationFormSheet];
  116. [self presentViewController:securityOptionsNC animated:YES completion:nil];
  117. }
  118. #pragma --------------------------------------------------------------------------------------------
  119. #pragma mark == BKPasscodeViewController ==
  120. #pragma --------------------------------------------------------------------------------------------
  121. - (void)passcodeViewController:(BKPasscodeViewController *)aViewController didFinishWithPasscode:(NSString *)aPasscode
  122. {
  123. switch (aViewController.type) {
  124. case BKPasscodeViewControllerNewPasscodeType: {
  125. // min passcode 4 chars
  126. if ([aPasscode length] >= 4) {
  127. [CCUtility setKeyChainPasscodeForUUID:[CCUtility getUUID] conPasscode:aPasscode];
  128. // verify
  129. NSString *pwd = [CCUtility getKeyChainPasscodeForUUID:[CCUtility getUUID]];
  130. if ([pwd isEqualToString:aPasscode] == NO || pwd == nil) {
  131. UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"_error_", nil) message:@"Fatal error writing key" delegate:nil cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(@"_ok_", nil), nil];
  132. [alertView show];
  133. } else {
  134. // Crypto Cloud Mode : Activated
  135. app.isCryptoCloudMode = YES;
  136. // force reload all directory for all users
  137. [CCCoreData clearAllDateReadDirectory];
  138. // Request : Send Passcode email
  139. [self performSelector:@selector(activateSecurityOptions) withObject:nil afterDelay:0.1];
  140. }
  141. } else {
  142. UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"_error_", nil) message:NSLocalizedString(@"_passcode_too_short_", nil) delegate:nil cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(@"_ok_", nil), nil];
  143. [alertView show];
  144. }
  145. [aViewController dismissViewControllerAnimated:YES completion:nil];
  146. }
  147. break;
  148. case BKPasscodeViewControllerCheckPasscodeType: {
  149. // Crypto Cloud Mode : Deactivated
  150. [CCUtility adminRemovePasscode];
  151. app.isCryptoCloudMode = NO;
  152. // force reload all directory for all users
  153. [CCCoreData clearAllDateReadDirectory];
  154. UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"_OK_", nil) message:@"Disattivazione avvenuta correttamente" 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