NCManageEndToEndEncryption.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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)initWithCoder:(NSCoder *)aDecoder
  35. {
  36. self = [super initWithCoder:aDecoder];
  37. if (self) {
  38. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadForm) name:@"reloadManageEndToEndEncryption" object:nil];
  39. [self initializeForm];
  40. }
  41. return self;
  42. }
  43. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  44. {
  45. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  46. if (self) {
  47. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadForm) name:@"reloadManageEndToEndEncryption" object:nil];
  48. [self initializeForm];
  49. }
  50. return self;
  51. }
  52. - (void)initializeForm
  53. {
  54. XLFormDescriptor *form ;
  55. XLFormSectionDescriptor *section;
  56. XLFormRowDescriptor *row;
  57. form = [XLFormDescriptor formDescriptorWithTitle:NSLocalizedString(@"_e2e_settings_", nil)];
  58. tableCapabilities *capabilities = [[NCManageDatabase sharedInstance] getCapabilites];
  59. if (capabilities.endToEndEncryption == NO) {
  60. // Section SERVICE NOT AVAILABLE -------------------------------------------------
  61. section = [XLFormSectionDescriptor formSection];
  62. [form addFormSection:section];
  63. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"serviceActivated" rowType:XLFormRowDescriptorTypeInfo title:NSLocalizedString(@"_e2e_settings_not_available_", nil)];
  64. [row.cellConfig setObject:[UIImage imageNamed:@"no_red"] forKey:@"imageView.image"];
  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. [section addFormRow:row];
  69. self.form = form;
  70. return;
  71. }
  72. if ([CCUtility isEndToEndEnabled:app.activeAccount]) {
  73. // Section SERVICE ACTIVATED -------------------------------------------------
  74. section = [XLFormSectionDescriptor formSection];
  75. [form addFormSection:section];
  76. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"serviceActivated" rowType:XLFormRowDescriptorTypeInfo title:NSLocalizedString(@"_e2e_settings_activated_", nil)];
  77. [row.cellConfig setObject:[UIImage imageNamed:@"ok_green"] forKey:@"imageView.image"];
  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. [section addFormRow:row];
  82. // Section PASSPHRASE -------------------------------------------------
  83. section = [XLFormSectionDescriptor formSectionWithTitle:NSLocalizedString(@"_e2e_settings_read_passphrase_", nil)];
  84. [form addFormSection:section];
  85. // Read Passphrase
  86. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"readPassphrase" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"_e2e_settings_read_passphrase_", nil)];
  87. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  88. [row.cellConfig setObject:[UIColor blackColor] forKey:@"textLabel.textColor"];
  89. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  90. row.action.formSelector = @selector(readPassphrase:);
  91. [section addFormRow:row];
  92. } else {
  93. // Section START E2E -------------------------------------------------
  94. section = [XLFormSectionDescriptor formSection];
  95. [form addFormSection:section];
  96. // Start e2e
  97. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"startE2E" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"_e2e_settings_start_", nil)];
  98. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  99. [row.cellConfig setObject:[UIColor blackColor] forKey:@"textLabel.textColor"];
  100. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  101. row.action.formSelector = @selector(startE2E:);
  102. [section addFormRow:row];
  103. }
  104. #ifdef DEBUG
  105. // Section DELETE KEYS -------------------------------------------------
  106. section = [XLFormSectionDescriptor formSectionWithTitle:NSLocalizedString(@"Delete server keys ", nil)];
  107. [form addFormSection:section];
  108. // Delete publicKey
  109. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"deletePublicKey" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"Delete PublicKey", nil)];
  110. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  111. [row.cellConfig setObject:[UIColor blackColor] forKey:@"textLabel.textColor"];
  112. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  113. row.action.formSelector = @selector(deletePublicKey:);
  114. [section addFormRow:row];
  115. // Delete privateKey
  116. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"deletePrivateKey" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"Delete PrivateKey", nil)];
  117. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  118. [row.cellConfig setObject:[UIColor blackColor] forKey:@"textLabel.textColor"];
  119. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  120. row.action.formSelector = @selector(deletePrivateKey:);
  121. [section addFormRow:row];
  122. // Delete locally Encryption
  123. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"deleteLocallyEncryption" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"Delete locally encryption", nil)];
  124. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  125. [row.cellConfig setObject:[UIColor blackColor] forKey:@"textLabel.textColor"];
  126. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  127. row.action.formSelector = @selector(deleteLocallyEncryption:);
  128. [section addFormRow:row];
  129. #endif
  130. self.form = form;
  131. }
  132. -(void)reloadForm
  133. {
  134. [self initializeForm];
  135. }
  136. #pragma --------------------------------------------------------------------------------------------
  137. #pragma mark === Action ===
  138. #pragma --------------------------------------------------------------------------------------------
  139. - (void)deletePublicKey:(XLFormRowDescriptor *)sender
  140. {
  141. [self deselectFormRow:sender];
  142. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:app.activeAccount];
  143. metadataNet.action = actionDeleteEndToEndPublicKey;
  144. [app addNetworkingOperationQueue:app.netQueue delegate:app.endToEndInterface metadataNet:metadataNet];
  145. }
  146. - (void)deletePrivateKey:(XLFormRowDescriptor *)sender
  147. {
  148. [self deselectFormRow:sender];
  149. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:app.activeAccount];
  150. metadataNet.action = actionDeleteEndToEndPrivateKey;
  151. [app addNetworkingOperationQueue:app.netQueue delegate:app.endToEndInterface metadataNet:metadataNet];
  152. }
  153. - (void)deleteLocallyEncryption:(XLFormRowDescriptor *)sender
  154. {
  155. [self deselectFormRow:sender];
  156. [CCUtility clearAllKeysEndToEnd:app.activeAccount];
  157. [self initializeForm];
  158. }
  159. - (void)startE2E:(XLFormRowDescriptor *)sender
  160. {
  161. [self deselectFormRow:sender];
  162. [app.endToEndInterface initEndToEndEncryption];
  163. }
  164. - (void)readPassphrase:(XLFormRowDescriptor *)sender
  165. {
  166. [self deselectFormRow:sender];
  167. if ([[CCUtility getBlockCode] length]) {
  168. CCBKPasscode *viewController = [[CCBKPasscode alloc] initWithNibName:nil bundle:nil];
  169. viewController.delegate = self;
  170. viewController.fromType = CCBKPasscodeFromCheckPassphrase;
  171. viewController.type = BKPasscodeViewControllerCheckPasscodeType;
  172. if ([CCUtility getSimplyBlockCode]) {
  173. viewController.passcodeStyle = BKPasscodeInputViewNumericPasscodeStyle;
  174. viewController.passcodeInputView.maximumLength = 6;
  175. } else {
  176. viewController.passcodeStyle = BKPasscodeInputViewNormalPasscodeStyle;
  177. viewController.passcodeInputView.maximumLength = 64;
  178. }
  179. BKTouchIDManager *touchIDManager = [[BKTouchIDManager alloc] initWithKeychainServiceName:k_serviceShareKeyChain];
  180. touchIDManager.promptText = NSLocalizedString(@"_scan_fingerprint_", nil);
  181. viewController.touchIDManager = touchIDManager;
  182. viewController.title = NSLocalizedString(@"_e2e_settings_read_passphrase_", nil);
  183. viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(passcodeViewCloseButtonPressed:)];
  184. viewController.navigationItem.leftBarButtonItem.tintColor = [NCBrandColor sharedInstance].encrypted;
  185. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
  186. [self presentViewController:navigationController animated:YES completion:nil];
  187. } else {
  188. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_info_", nil) message:NSLocalizedString(@"_e2e_settings_lock_not_active_", nil) preferredStyle:UIAlertControllerStyleAlert];
  189. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"OK action") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  190. }];
  191. [alertController addAction:okAction];
  192. [self presentViewController:alertController animated:YES completion:nil];
  193. }
  194. }
  195. #pragma --------------------------------------------------------------------------------------------
  196. #pragma mark === BKPasscodeViewController ===
  197. #pragma --------------------------------------------------------------------------------------------
  198. - (NSUInteger)passcodeViewControllerNumberOfFailedAttempts:(CCBKPasscode *)aViewController
  199. {
  200. return _failedAttempts;
  201. }
  202. - (NSDate *)passcodeViewControllerLockUntilDate:(CCBKPasscode *)aViewController
  203. {
  204. return _lockUntilDate;
  205. }
  206. - (void)passcodeViewCloseButtonPressed:(id)sender
  207. {
  208. [self dismissViewControllerAnimated:YES completion:nil];
  209. }
  210. - (void)passcodeViewController:(CCBKPasscode *)aViewController authenticatePasscode:(NSString *)aPasscode resultHandler:(void (^)(BOOL))aResultHandler
  211. {
  212. if ([aPasscode isEqualToString:[CCUtility getBlockCode]]) {
  213. _lockUntilDate = nil;
  214. _failedAttempts = 0;
  215. aResultHandler(YES);
  216. } else
  217. aResultHandler(NO);
  218. }
  219. - (void)passcodeViewController:(CCBKPasscode *)aViewController didFinishWithPasscode:(NSString *)aPasscode
  220. {
  221. [aViewController dismissViewControllerAnimated:YES completion:nil];
  222. NSString *e2ePassphrase = [CCUtility getEndToEndPassphrase:app.activeAccount];
  223. NSLog(@"[LOG] Passphrase: %@", e2ePassphrase);
  224. NSString *message = [NSString stringWithFormat:@"\n%@\n\n\n%@", NSLocalizedString(@"_e2e_settings_the_passphrase_is_", nil), e2ePassphrase];
  225. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_info_", nil) message:message preferredStyle:UIAlertControllerStyleAlert];
  226. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"OK action") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  227. }];
  228. [alertController addAction:okAction];
  229. [self presentViewController:alertController animated:YES completion:nil];
  230. }
  231. @end