NCManageEndToEndEncryption.m 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. //
  2. // NCManageEndToEndEncryption.m
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 13/10/17.
  6. // Copyright © 2017 Marino Faggiana. 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 () <NCEndToEndInitializeDelegate>
  28. {
  29. AppDelegate *appDelegate;
  30. NSUInteger _failedAttempts;
  31. NSDate *_lockUntilDate;
  32. }
  33. @end
  34. @implementation NCManageEndToEndEncryption
  35. - (id)initWithCoder:(NSCoder *)aDecoder
  36. {
  37. self = [super initWithCoder:aDecoder];
  38. if (self) {
  39. appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  40. [self initializeForm];
  41. }
  42. return self;
  43. }
  44. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  45. {
  46. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  47. if (self) {
  48. appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  49. [self initializeForm];
  50. }
  51. return self;
  52. }
  53. - (void)initializeForm
  54. {
  55. XLFormDescriptor *form ;
  56. XLFormSectionDescriptor *section;
  57. XLFormRowDescriptor *row;
  58. form = [XLFormDescriptor formDescriptorWithTitle:NSLocalizedString(@"_e2e_settings_", nil)];
  59. tableCapabilities *capabilities = [[NCManageDatabase sharedInstance] getCapabilites];
  60. if (capabilities.endToEndEncryption == NO) {
  61. // Section SERVICE NOT AVAILABLE -------------------------------------------------
  62. section = [XLFormSectionDescriptor formSection];
  63. [form addFormSection:section];
  64. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"serviceActivated" rowType:XLFormRowDescriptorTypeInfo title:NSLocalizedString(@"_e2e_settings_not_available_", nil)];
  65. [row.cellConfig setObject:[UIImage imageNamed:@"no_red"] forKey:@"imageView.image"];
  66. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  67. [row.cellConfig setObject:[UIColor blackColor] forKey:@"textLabel.textColor"];
  68. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  69. [section addFormRow:row];
  70. self.form = form;
  71. return;
  72. }
  73. if ([CCUtility isEndToEndEnabled:appDelegate.activeAccount]) {
  74. // Section SERVICE ACTIVATED -------------------------------------------------
  75. section = [XLFormSectionDescriptor formSection];
  76. [form addFormSection:section];
  77. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"serviceActivated" rowType:XLFormRowDescriptorTypeInfo title:NSLocalizedString(@"_e2e_settings_activated_", nil)];
  78. [row.cellConfig setObject:[UIImage imageNamed:@"ok_green"] forKey:@"imageView.image"];
  79. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  80. [row.cellConfig setObject:[UIColor blackColor] forKey:@"textLabel.textColor"];
  81. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  82. [section addFormRow:row];
  83. // Section PASSPHRASE -------------------------------------------------
  84. section = [XLFormSectionDescriptor formSection];
  85. [form addFormSection:section];
  86. // Read Passphrase
  87. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"readPassphrase" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"_e2e_settings_read_passphrase_", nil)];
  88. [row.cellConfig setObject:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"e2eReadPassphrase"] multiplier:2 color:[NCBrandColor sharedInstance].icon] forKey:@"imageView.image"];
  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(readPassphrase:);
  93. [section addFormRow:row];
  94. // Section DELETE -------------------------------------------------
  95. section = [XLFormSectionDescriptor formSection];
  96. [form addFormSection:section];
  97. // remove locally Encryption
  98. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"removeLocallyEncryption" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"_e2e_settings_remove_", nil)];
  99. [row.cellConfig setObject:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"lock"] multiplier:2 color:[NCBrandColor sharedInstance].icon] forKey:@"imageView.image"];
  100. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  101. [row.cellConfig setObject:[UIColor blackColor] forKey:@"textLabel.textColor"];
  102. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  103. row.action.formSelector = @selector(removeLocallyEncryption:);
  104. [section addFormRow:row];
  105. } else {
  106. // Section START E2E -------------------------------------------------
  107. section = [XLFormSectionDescriptor formSection];
  108. [form addFormSection:section];
  109. // Start e2e
  110. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"startE2E" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"_e2e_settings_start_", nil)];
  111. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  112. [row.cellConfig setObject:[UIColor blackColor] forKey:@"textLabel.textColor"];
  113. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  114. row.action.formSelector = @selector(startE2E:);
  115. [section addFormRow:row];
  116. }
  117. #ifdef DEBUG
  118. // Section DELETE KEYS -------------------------------------------------
  119. section = [XLFormSectionDescriptor formSectionWithTitle:NSLocalizedString(@"Delete server keys ", nil)];
  120. [form addFormSection:section];
  121. // Delete publicKey
  122. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"deletePublicKey" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"Delete PublicKey", nil)];
  123. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  124. [row.cellConfig setObject:[UIColor blackColor] forKey:@"textLabel.textColor"];
  125. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  126. row.action.formSelector = @selector(deletePublicKey:);
  127. [section addFormRow:row];
  128. // Delete privateKey
  129. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"deletePrivateKey" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"Delete PrivateKey", nil)];
  130. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  131. [row.cellConfig setObject:[UIColor blackColor] forKey:@"textLabel.textColor"];
  132. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  133. row.action.formSelector = @selector(deletePrivateKey:);
  134. [section addFormRow:row];
  135. #endif
  136. self.form = form;
  137. }
  138. - (void)viewDidLoad
  139. {
  140. [super viewDidLoad];
  141. // E2EE
  142. self.endToEndInitialize = [NCEndToEndInitialize new];
  143. self.endToEndInitialize.delegate = self;
  144. }
  145. #pragma --------------------------------------------------------------------------------------------
  146. #pragma mark === Action ===
  147. #pragma --------------------------------------------------------------------------------------------
  148. - (void)startE2E:(XLFormRowDescriptor *)sender
  149. {
  150. [self deselectFormRow:sender];
  151. if ([[CCUtility getBlockCode] length]) {
  152. CCBKPasscode *viewController = [[CCBKPasscode alloc] initWithNibName:nil bundle:nil];
  153. viewController.delegate = self;
  154. viewController.fromType = CCBKPasscodeFromStartEncryption;
  155. viewController.type = BKPasscodeViewControllerCheckPasscodeType;
  156. if ([CCUtility getSimplyBlockCode]) {
  157. viewController.passcodeStyle = BKPasscodeInputViewNumericPasscodeStyle;
  158. viewController.passcodeInputView.maximumLength = 6;
  159. } else {
  160. viewController.passcodeStyle = BKPasscodeInputViewNormalPasscodeStyle;
  161. viewController.passcodeInputView.maximumLength = 64;
  162. }
  163. BKTouchIDManager *touchIDManager = [[BKTouchIDManager alloc] initWithKeychainServiceName:k_serviceShareKeyChain];
  164. touchIDManager.promptText = NSLocalizedString(@"_scan_fingerprint_", nil);
  165. viewController.touchIDManager = touchIDManager;
  166. viewController.title = NSLocalizedString(@"_e2e_settings_start_", nil);
  167. viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(passcodeViewCloseButtonPressed:)];
  168. viewController.navigationItem.leftBarButtonItem.tintColor = [UIColor blackColor];
  169. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
  170. [self presentViewController:navigationController animated:YES completion:nil];
  171. } else {
  172. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_info_", nil) message:NSLocalizedString(@"_e2e_settings_lock_not_active_", nil) preferredStyle:UIAlertControllerStyleAlert];
  173. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"OK action") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  174. }];
  175. [alertController addAction:okAction];
  176. [self presentViewController:alertController animated:YES completion:nil];
  177. }
  178. }
  179. - (void)readPassphrase:(XLFormRowDescriptor *)sender
  180. {
  181. [self deselectFormRow:sender];
  182. if ([[CCUtility getBlockCode] length]) {
  183. CCBKPasscode *viewController = [[CCBKPasscode alloc] initWithNibName:nil bundle:nil];
  184. viewController.delegate = self;
  185. viewController.fromType = CCBKPasscodeFromCheckPassphrase;
  186. viewController.type = BKPasscodeViewControllerCheckPasscodeType;
  187. if ([CCUtility getSimplyBlockCode]) {
  188. viewController.passcodeStyle = BKPasscodeInputViewNumericPasscodeStyle;
  189. viewController.passcodeInputView.maximumLength = 6;
  190. } else {
  191. viewController.passcodeStyle = BKPasscodeInputViewNormalPasscodeStyle;
  192. viewController.passcodeInputView.maximumLength = 64;
  193. }
  194. BKTouchIDManager *touchIDManager = [[BKTouchIDManager alloc] initWithKeychainServiceName:k_serviceShareKeyChain];
  195. touchIDManager.promptText = NSLocalizedString(@"_scan_fingerprint_", nil);
  196. viewController.touchIDManager = touchIDManager;
  197. viewController.title = NSLocalizedString(@"_e2e_settings_read_passphrase_", nil);
  198. viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(passcodeViewCloseButtonPressed:)];
  199. viewController.navigationItem.leftBarButtonItem.tintColor = [UIColor blackColor];
  200. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
  201. [self presentViewController:navigationController animated:YES completion:nil];
  202. } else {
  203. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_info_", nil) message:NSLocalizedString(@"_e2e_settings_lock_not_active_", nil) preferredStyle:UIAlertControllerStyleAlert];
  204. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"OK action") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  205. }];
  206. [alertController addAction:okAction];
  207. [self presentViewController:alertController animated:YES completion:nil];
  208. }
  209. }
  210. - (void)removeLocallyEncryption:(XLFormRowDescriptor *)sender
  211. {
  212. [self deselectFormRow:sender];
  213. if ([[CCUtility getBlockCode] length]) {
  214. CCBKPasscode *viewController = [[CCBKPasscode alloc] initWithNibName:nil bundle:nil];
  215. viewController.delegate = self;
  216. viewController.fromType = CCBKPasscodeFromRemoveEncryption;
  217. viewController.type = BKPasscodeViewControllerCheckPasscodeType;
  218. if ([CCUtility getSimplyBlockCode]) {
  219. viewController.passcodeStyle = BKPasscodeInputViewNumericPasscodeStyle;
  220. viewController.passcodeInputView.maximumLength = 6;
  221. } else {
  222. viewController.passcodeStyle = BKPasscodeInputViewNormalPasscodeStyle;
  223. viewController.passcodeInputView.maximumLength = 64;
  224. }
  225. BKTouchIDManager *touchIDManager = [[BKTouchIDManager alloc] initWithKeychainServiceName:k_serviceShareKeyChain];
  226. touchIDManager.promptText = NSLocalizedString(@"_scan_fingerprint_", nil);
  227. viewController.touchIDManager = touchIDManager;
  228. viewController.title = NSLocalizedString(@"_e2e_settings_remove_", nil);
  229. viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(passcodeViewCloseButtonPressed:)];
  230. viewController.navigationItem.leftBarButtonItem.tintColor = [UIColor blackColor];
  231. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
  232. [self presentViewController:navigationController animated:YES completion:nil];
  233. } else {
  234. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_info_", nil) message:NSLocalizedString(@"_e2e_settings_lock_not_active_", nil) preferredStyle:UIAlertControllerStyleAlert];
  235. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"OK action") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  236. }];
  237. [alertController addAction:okAction];
  238. [self presentViewController:alertController animated:YES completion:nil];
  239. }
  240. }
  241. - (void)deletePublicKey:(XLFormRowDescriptor *)sender
  242. {
  243. [self deselectFormRow:sender];
  244. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:appDelegate.activeAccount];
  245. metadataNet.action = actionDeleteEndToEndPublicKey;
  246. [appDelegate addNetworkingOperationQueue:appDelegate.netQueue delegate:self metadataNet:metadataNet];
  247. }
  248. - (void)deletePrivateKey:(XLFormRowDescriptor *)sender
  249. {
  250. [self deselectFormRow:sender];
  251. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:appDelegate.activeAccount];
  252. metadataNet.action = actionDeleteEndToEndPrivateKey;
  253. [appDelegate addNetworkingOperationQueue:appDelegate.netQueue delegate:self metadataNet:metadataNet];
  254. }
  255. #pragma --------------------------------------------------------------------------------------------
  256. #pragma mark === Delegate ===
  257. #pragma --------------------------------------------------------------------------------------------
  258. - (void)endToEndInitializeSuccess
  259. {
  260. // Reload All Datasource
  261. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:@"clearDateReadDataSource" object:nil];
  262. [self initializeForm];
  263. }
  264. - (void)deleteEndToEndPrivateKeySuccess:(CCMetadataNet *)metadataNet
  265. {
  266. [appDelegate messageNotification:@"E2E delete privateKey" description:@"Success" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeSuccess errorCode:0];
  267. }
  268. - (void)deleteEndToEndPrivateKeyFailure:(CCMetadataNet *)metadataNet message:(NSString *)message errorCode:(NSInteger)errorCode
  269. {
  270. [appDelegate messageNotification:@"E2E delete privateKey" description:message visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:k_CCErrorInternalError];
  271. }
  272. - (void)deleteEndToEndPublicKeySuccess:(CCMetadataNet *)metadataNet
  273. {
  274. [appDelegate messageNotification:@"E2E delete publicKey" description:@"Success" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeSuccess errorCode:0];
  275. }
  276. - (void)deleteEndToEndPublicKeyFailure:(CCMetadataNet *)metadataNet message:(NSString *)message errorCode:(NSInteger)errorCode
  277. {
  278. [appDelegate messageNotification:@"E2E delete publicKey" description:message visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:k_CCErrorInternalError];
  279. }
  280. #pragma --------------------------------------------------------------------------------------------
  281. #pragma mark === BKPasscodeViewController ===
  282. #pragma --------------------------------------------------------------------------------------------
  283. - (NSUInteger)passcodeViewControllerNumberOfFailedAttempts:(CCBKPasscode *)aViewController
  284. {
  285. return _failedAttempts;
  286. }
  287. - (NSDate *)passcodeViewControllerLockUntilDate:(CCBKPasscode *)aViewController
  288. {
  289. return _lockUntilDate;
  290. }
  291. - (void)passcodeViewCloseButtonPressed:(id)sender
  292. {
  293. [self dismissViewControllerAnimated:YES completion:nil];
  294. }
  295. - (void)passcodeViewController:(CCBKPasscode *)aViewController authenticatePasscode:(NSString *)aPasscode resultHandler:(void (^)(BOOL))aResultHandler
  296. {
  297. if ([aPasscode isEqualToString:[CCUtility getBlockCode]]) {
  298. _lockUntilDate = nil;
  299. _failedAttempts = 0;
  300. aResultHandler(YES);
  301. } else
  302. aResultHandler(NO);
  303. }
  304. - (void)passcodeViewController:(CCBKPasscode *)aViewController didFinishWithPasscode:(NSString *)aPasscode
  305. {
  306. [aViewController dismissViewControllerAnimated:YES completion:nil];
  307. if (aViewController.fromType == CCBKPasscodeFromStartEncryption) {
  308. [self.endToEndInitialize initEndToEndEncryption];
  309. }
  310. if (aViewController.fromType == CCBKPasscodeFromCheckPassphrase) {
  311. NSString *e2ePassphrase = [CCUtility getEndToEndPassphrase:appDelegate.activeAccount];
  312. NSLog(@"[LOG] Passphrase: %@", e2ePassphrase);
  313. NSString *message = [NSString stringWithFormat:@"\n%@\n\n\n%@", NSLocalizedString(@"_e2e_settings_the_passphrase_is_", nil), e2ePassphrase];
  314. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_info_", nil) message:message preferredStyle:UIAlertControllerStyleAlert];
  315. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"OK action") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  316. }];
  317. [alertController addAction:okAction];
  318. [self presentViewController:alertController animated:YES completion:nil];
  319. }
  320. if (aViewController.fromType == CCBKPasscodeFromRemoveEncryption) {
  321. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_e2e_settings_remove_", nil) message:NSLocalizedString(@"_e2e_settings_remove_message_", nil) preferredStyle:UIAlertControllerStyleAlert];
  322. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"_remove_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  323. [CCUtility clearAllKeysEndToEnd:appDelegate.activeAccount];
  324. [self initializeForm];
  325. }];
  326. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_",nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  327. NSLog(@"[LOG] Cancel action");
  328. }];
  329. [alertController addAction:okAction];
  330. [alertController addAction:cancelAction];
  331. [self presentViewController:alertController animated:YES completion:nil];
  332. }
  333. }
  334. @end