NCManageEndToEndEncryption.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. //
  2. // NCManageEndToEndEncryption.m
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 13/10/17.
  6. // Copyright © 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 "NCManageEndToEndEncryption.h"
  24. #import "AppDelegate.h"
  25. #import "CCNetworking.h"
  26. #import "NCBridgeSwift.h"
  27. @interface NCManageEndToEndEncryption ()
  28. {
  29. NSUInteger _failedAttempts;
  30. NSDate *_lockUntilDate;
  31. }
  32. @end
  33. @implementation NCManageEndToEndEncryption
  34. -(id)init
  35. {
  36. XLFormDescriptor *form ;
  37. XLFormSectionDescriptor *section;
  38. XLFormRowDescriptor *row;
  39. form = [XLFormDescriptor formDescriptorWithTitle:NSLocalizedString(@"_e2e_settings_", nil)];
  40. tableCapabilities *capabilities = [[NCManageDatabase sharedInstance] getCapabilites];
  41. if (capabilities.endToEndEncryption == NO) {
  42. section = [XLFormSectionDescriptor formSectionWithTitle:NSLocalizedString(@"_e2e_settings_not_available_", nil)];
  43. [form addFormSection:section];
  44. return [super initWithForm:form];
  45. }
  46. if ([CCUtility isEndToEndEnabled:app.activeAccount]) {
  47. section = [XLFormSectionDescriptor formSectionWithTitle:NSLocalizedString(@"_e2e_settings_activated_", nil)];
  48. [form addFormSection:section];
  49. // Section PASSPHRASE -------------------------------------------------
  50. section = [XLFormSectionDescriptor formSectionWithTitle:NSLocalizedString(@"_e2e_settings_read_passphrase_", nil)];
  51. [form addFormSection:section];
  52. // Read Passphrase
  53. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"readPassphrase" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"_e2e_settings_read_passphrase_", nil)];
  54. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  55. [row.cellConfig setObject:[UIColor blackColor] forKey:@"textLabel.textColor"];
  56. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  57. row.action.formSelector = @selector(readPassphrase:);
  58. [section addFormRow:row];
  59. } else {
  60. // Section START E2E -------------------------------------------------
  61. section = [XLFormSectionDescriptor formSectionWithTitle:NSLocalizedString(@"_e2e_settings_start_", nil)];
  62. [form addFormSection:section];
  63. // Start e2e
  64. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"startE2E" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"_e2e_settings_start_", nil)];
  65. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  66. [row.cellConfig setObject:[UIColor blackColor] forKey:@"textLabel.textColor"];
  67. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  68. row.action.formSelector = @selector(startE2E:);
  69. [section addFormRow:row];
  70. }
  71. #ifdef DEBUG
  72. // Section DELETE KEYS -------------------------------------------------
  73. section = [XLFormSectionDescriptor formSectionWithTitle:NSLocalizedString(@"Delete server keys ", nil)];
  74. [form addFormSection:section];
  75. // Delete publicKey
  76. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"deletePublicKey" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"Delete PublicKey", nil)];
  77. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  78. [row.cellConfig setObject:[UIColor blackColor] forKey:@"textLabel.textColor"];
  79. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  80. row.action.formSelector = @selector(deletePublicKey:);
  81. [section addFormRow:row];
  82. // Delete privateKey
  83. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"deletePrivateKey" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"Delete PrivateKey", nil)];
  84. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  85. [row.cellConfig setObject:[UIColor blackColor] forKey:@"textLabel.textColor"];
  86. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  87. row.action.formSelector = @selector(deletePrivateKey:);
  88. [section addFormRow:row];
  89. // Delete locally Encryption
  90. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"deleteLocallyEncryption" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"Delete locally encryption", nil)];
  91. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  92. [row.cellConfig setObject:[UIColor blackColor] forKey:@"textLabel.textColor"];
  93. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  94. row.action.formSelector = @selector(deleteLocallyEncryption:);
  95. [section addFormRow:row];
  96. #endif
  97. return [super initWithForm:form];
  98. }
  99. - (void)deletePublicKey:(XLFormRowDescriptor *)sender
  100. {
  101. [self deselectFormRow:sender];
  102. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:app.activeAccount];
  103. metadataNet.action = actionDeleteEndToEndPublicKey;
  104. [app addNetworkingOperationQueue:app.netQueue delegate:app.endToEndInterface metadataNet:metadataNet];
  105. }
  106. - (void)deletePrivateKey:(XLFormRowDescriptor *)sender
  107. {
  108. [self deselectFormRow:sender];
  109. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:app.activeAccount];
  110. metadataNet.action = actionDeleteEndToEndPrivateKey;
  111. [app addNetworkingOperationQueue:app.netQueue delegate:app.endToEndInterface metadataNet:metadataNet];
  112. }
  113. - (void)deleteLocallyEncryption:(XLFormRowDescriptor *)sender
  114. {
  115. [self deselectFormRow:sender];
  116. [CCUtility initEndToEnd:app.activeAccount];
  117. }
  118. - (void)startE2E:(XLFormRowDescriptor *)sender
  119. {
  120. [self deselectFormRow:sender];
  121. [CCUtility initEndToEnd:app.activeAccount];
  122. [app.endToEndInterface initEndToEndEncryption];
  123. }
  124. - (void)readPassphrase:(XLFormRowDescriptor *)sender
  125. {
  126. [self deselectFormRow:sender];
  127. if ([[CCUtility getBlockCode] length]) {
  128. CCBKPasscode *viewController = [[CCBKPasscode alloc] initWithNibName:nil bundle:nil];
  129. viewController.delegate = self;
  130. viewController.fromType = CCBKPasscodeFromCheckPassphrase;
  131. viewController.type = BKPasscodeViewControllerCheckPasscodeType;
  132. if ([CCUtility getSimplyBlockCode]) {
  133. viewController.passcodeStyle = BKPasscodeInputViewNumericPasscodeStyle;
  134. viewController.passcodeInputView.maximumLength = 6;
  135. } else {
  136. viewController.passcodeStyle = BKPasscodeInputViewNormalPasscodeStyle;
  137. viewController.passcodeInputView.maximumLength = 64;
  138. }
  139. BKTouchIDManager *touchIDManager = [[BKTouchIDManager alloc] initWithKeychainServiceName:k_serviceShareKeyChain];
  140. touchIDManager.promptText = NSLocalizedString(@"_scan_fingerprint_", nil);
  141. viewController.touchIDManager = touchIDManager;
  142. viewController.title = NSLocalizedString(@"_e2e_settings_read_passphrase_", nil);
  143. viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(passcodeViewCloseButtonPressed:)];
  144. viewController.navigationItem.leftBarButtonItem.tintColor = [NCBrandColor sharedInstance].encrypted;
  145. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
  146. [self presentViewController:navigationController animated:YES completion:nil];
  147. } else {
  148. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_info_", nil) message:NSLocalizedString(@"_e2e_settings_lock_not_active_", nil) preferredStyle:UIAlertControllerStyleAlert];
  149. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"OK action") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  150. }];
  151. [alertController addAction:okAction];
  152. [self presentViewController:alertController animated:YES completion:nil];
  153. }
  154. }
  155. #pragma --------------------------------------------------------------------------------------------
  156. #pragma mark === BKPasscodeViewController ===
  157. #pragma --------------------------------------------------------------------------------------------
  158. - (NSUInteger)passcodeViewControllerNumberOfFailedAttempts:(CCBKPasscode *)aViewController
  159. {
  160. return _failedAttempts;
  161. }
  162. - (NSDate *)passcodeViewControllerLockUntilDate:(CCBKPasscode *)aViewController
  163. {
  164. return _lockUntilDate;
  165. }
  166. - (void)passcodeViewCloseButtonPressed:(id)sender
  167. {
  168. [self dismissViewControllerAnimated:YES completion:nil];
  169. }
  170. - (void)passcodeViewController:(CCBKPasscode *)aViewController authenticatePasscode:(NSString *)aPasscode resultHandler:(void (^)(BOOL))aResultHandler
  171. {
  172. if ([aPasscode isEqualToString:[CCUtility getBlockCode]]) {
  173. _lockUntilDate = nil;
  174. _failedAttempts = 0;
  175. aResultHandler(YES);
  176. } else
  177. aResultHandler(NO);
  178. }
  179. - (void)passcodeViewController:(CCBKPasscode *)aViewController didFinishWithPasscode:(NSString *)aPasscode
  180. {
  181. [aViewController dismissViewControllerAnimated:YES completion:nil];
  182. NSString *message = [NSString stringWithFormat:@"\n%@\n\n\n%@", NSLocalizedString(@"_e2e_settings_the_passphrase_is_", nil), [CCUtility getEndToEndPassphrase:app.activeAccount]];
  183. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_info_", nil) message:message preferredStyle:UIAlertControllerStyleAlert];
  184. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"OK action") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  185. }];
  186. [alertController addAction:okAction];
  187. [self presentViewController:alertController animated:YES completion:nil];
  188. }
  189. @end