NCManageEndToEndEncryption.m 10 KB

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