NCManageEndToEndEncryption.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. // Section INITIALIZE -------------------------------------------------
  48. section = [XLFormSectionDescriptor formSectionWithTitle:NSLocalizedString(@"_e2e_settings_initialize_", nil)];
  49. [form addFormSection:section];
  50. // Inizializze e2e
  51. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"initE2E" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"_e2e_settings_initialize_", nil)];
  52. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  53. [row.cellConfig setObject:[UIColor blackColor] forKey:@"textLabel.textColor"];
  54. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  55. row.action.formSelector = @selector(initE2E:);
  56. [section addFormRow:row];
  57. if ([CCUtility isEndToEndEnabled:app.activeAccount]) {
  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. }
  69. #ifdef DEBUG
  70. // Section DELETE KEYS -------------------------------------------------
  71. section = [XLFormSectionDescriptor formSectionWithTitle:NSLocalizedString(@"Delete server keys ", nil)];
  72. [form addFormSection:section];
  73. // Delete publicKey
  74. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"deletePublicKey" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"Delete PublicKey", nil)];
  75. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  76. [row.cellConfig setObject:[UIColor blackColor] forKey:@"textLabel.textColor"];
  77. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  78. row.action.formSelector = @selector(deletePublicKey:);
  79. [section addFormRow:row];
  80. // Delete privateKey
  81. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"deletePrivateKey" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"Delete PrivateKey", nil)];
  82. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  83. [row.cellConfig setObject:[UIColor blackColor] forKey:@"textLabel.textColor"];
  84. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  85. row.action.formSelector = @selector(deletePrivateKey:);
  86. [section addFormRow:row];
  87. // Delete locally Encryption
  88. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"deleteLocallyEncryption" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"Delete locally encryption", nil)];
  89. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  90. [row.cellConfig setObject:[UIColor blackColor] forKey:@"textLabel.textColor"];
  91. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  92. row.action.formSelector = @selector(deleteLocallyEncryption:);
  93. [section addFormRow:row];
  94. #endif
  95. return [super initWithForm:form];
  96. }
  97. - (void)deletePublicKey:(XLFormRowDescriptor *)sender
  98. {
  99. [self deselectFormRow:sender];
  100. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:app.activeAccount];
  101. metadataNet.action = actionDeleteEndToEndPublicKey;
  102. [app addNetworkingOperationQueue:app.netQueue delegate:app.endToEndInterface metadataNet:metadataNet];
  103. }
  104. - (void)deletePrivateKey:(XLFormRowDescriptor *)sender
  105. {
  106. [self deselectFormRow:sender];
  107. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:app.activeAccount];
  108. metadataNet.action = actionDeleteEndToEndPrivateKey;
  109. [app addNetworkingOperationQueue:app.netQueue delegate:app.endToEndInterface metadataNet:metadataNet];
  110. }
  111. - (void)deleteLocallyEncryption:(XLFormRowDescriptor *)sender
  112. {
  113. [self deselectFormRow:sender];
  114. [CCUtility initEndToEnd:app.activeAccount];
  115. }
  116. - (void)initE2E:(XLFormRowDescriptor *)sender
  117. {
  118. NSString *message;
  119. [self deselectFormRow:sender];
  120. // select Passphrase
  121. //app.e2ePassphrase = k_passphrase_test;
  122. app.e2ePassphrase = [NYMnemonic generateMnemonicString:@128 language:@"english"];
  123. if ([CCUtility isEndToEndEnabled:app.activeAccount]) {
  124. message = [NSString stringWithFormat:@"%@\n\n%@\n\n%@", NSLocalizedString(@"_e2e_settings_initialize_already_request_", nil), NSLocalizedString(@"_e2e_settings_view_passphrase_", nil), app.e2ePassphrase];
  125. } else {
  126. message = [NSString stringWithFormat:@"%@\n\n%@\n\n%@", NSLocalizedString(@"_e2e_settings_initialize_request_", nil), NSLocalizedString(@"_e2e_settings_view_passphrase_", nil), app.e2ePassphrase];
  127. }
  128. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_initialization_", nil) message:message preferredStyle:UIAlertControllerStyleAlert];
  129. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", @"Cancel action") style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  130. NSLog(@"Cancel action");
  131. }];
  132. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"OK action") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  133. [self deletePublicKey:sender];
  134. [self deletePrivateKey:sender];
  135. [CCUtility initEndToEnd:app.activeAccount];
  136. }];
  137. [alertController addAction:cancelAction];
  138. [alertController addAction:okAction];
  139. [self presentViewController:alertController animated:YES completion:nil];
  140. }
  141. - (void)readPassphrase:(XLFormRowDescriptor *)sender
  142. {
  143. [self deselectFormRow:sender];
  144. if ([[CCUtility getBlockCode] length]) {
  145. CCBKPasscode *viewController = [[CCBKPasscode alloc] initWithNibName:nil bundle:nil];
  146. viewController.delegate = self;
  147. viewController.fromType = CCBKPasscodeFromCheckPassphrase;
  148. viewController.type = BKPasscodeViewControllerCheckPasscodeType;
  149. if ([CCUtility getSimplyBlockCode]) {
  150. viewController.passcodeStyle = BKPasscodeInputViewNumericPasscodeStyle;
  151. viewController.passcodeInputView.maximumLength = 6;
  152. } else {
  153. viewController.passcodeStyle = BKPasscodeInputViewNormalPasscodeStyle;
  154. viewController.passcodeInputView.maximumLength = 64;
  155. }
  156. BKTouchIDManager *touchIDManager = [[BKTouchIDManager alloc] initWithKeychainServiceName:k_serviceShareKeyChain];
  157. touchIDManager.promptText = NSLocalizedString(@"_scan_fingerprint_", nil);
  158. viewController.touchIDManager = touchIDManager;
  159. viewController.title = NSLocalizedString(@"_e2e_settings_read_passphrase_", nil);
  160. viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(passcodeViewCloseButtonPressed:)];
  161. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
  162. [self presentViewController:navigationController animated:YES completion:nil];
  163. } else {
  164. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_info_", nil) message:NSLocalizedString(@"_e2e_settings_lock_not_active_", nil) preferredStyle:UIAlertControllerStyleAlert];
  165. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"OK action") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  166. }];
  167. [alertController addAction:okAction];
  168. [self presentViewController:alertController animated:YES completion:nil];
  169. }
  170. }
  171. #pragma --------------------------------------------------------------------------------------------
  172. #pragma mark === BKPasscodeViewController ===
  173. #pragma --------------------------------------------------------------------------------------------
  174. - (NSUInteger)passcodeViewControllerNumberOfFailedAttempts:(CCBKPasscode *)aViewController
  175. {
  176. return _failedAttempts;
  177. }
  178. - (NSDate *)passcodeViewControllerLockUntilDate:(CCBKPasscode *)aViewController
  179. {
  180. return _lockUntilDate;
  181. }
  182. - (void)passcodeViewCloseButtonPressed:(id)sender
  183. {
  184. [self dismissViewControllerAnimated:YES completion:nil];
  185. }
  186. - (void)passcodeViewController:(CCBKPasscode *)aViewController authenticatePasscode:(NSString *)aPasscode resultHandler:(void (^)(BOOL))aResultHandler
  187. {
  188. if ([aPasscode isEqualToString:[CCUtility getBlockCode]]) {
  189. _lockUntilDate = nil;
  190. _failedAttempts = 0;
  191. aResultHandler(YES);
  192. } else
  193. aResultHandler(NO);
  194. }
  195. - (void)passcodeViewController:(CCBKPasscode *)aViewController didFinishWithPasscode:(NSString *)aPasscode
  196. {
  197. [aViewController dismissViewControllerAnimated:YES completion:nil];
  198. NSString *message = [NSString stringWithFormat:@"%@\n%@", NSLocalizedString(@"_e2e_settings_the_passphrase_is_", nil), [CCUtility getEndToEndPassphrase:app.activeAccount]];
  199. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_info_", nil) message:message preferredStyle:UIAlertControllerStyleAlert];
  200. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"OK action") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  201. }];
  202. [alertController addAction:okAction];
  203. [self presentViewController:alertController animated:YES completion:nil];
  204. }
  205. @end