NCManageEndToEndEncryption.m 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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 <marino.faggiana@nextcloud.com>
  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 <TOPasscodeViewController/TOPasscodeViewController.h>
  27. #import "NCBridgeSwift.h"
  28. @interface NCManageEndToEndEncryption () <NCEndToEndInitializeDelegate, TOPasscodeViewControllerDelegate>
  29. {
  30. AppDelegate *appDelegate;
  31. NSString *passcodeType;
  32. TOPasscodeViewController *passcodeViewController;
  33. }
  34. @end
  35. @implementation NCManageEndToEndEncryption
  36. - (void)initializeForm
  37. {
  38. XLFormDescriptor *form = [XLFormDescriptor formDescriptor];
  39. XLFormSectionDescriptor *section;
  40. XLFormRowDescriptor *row;
  41. BOOL isE2EEEnabled = [[NCManageDatabase sharedInstance] getCapabilitiesServerBoolWithAccount:appDelegate.activeAccount elements:NCElementsJSON.shared.capabilitiesE2EEEnabled exists:false];
  42. if (isE2EEEnabled == NO) {
  43. // Section SERVICE NOT AVAILABLE -------------------------------------------------
  44. section = [XLFormSectionDescriptor formSection];
  45. [form addFormSection:section];
  46. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"serviceActivated" rowType:XLFormRowDescriptorTypeInfo title:NSLocalizedString(@"_e2e_settings_not_available_", nil)];
  47. row.cellConfigAtConfigure[@"backgroundColor"] = NCBrandColor.sharedInstance.backgroundView;
  48. [row.cellConfig setObject:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"closeCircle"] width:50 height:50 color:[UIColor redColor]] forKey:@"imageView.image"];
  49. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0] forKey:@"textLabel.font"];
  50. [row.cellConfig setObject:NCBrandColor.sharedInstance.textView forKey:@"textLabel.textColor"];
  51. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  52. [section addFormRow:row];
  53. self.tableView.showsVerticalScrollIndicator = NO;
  54. self.form = form;
  55. return;
  56. }
  57. if ([CCUtility isEndToEndEnabled:appDelegate.activeAccount]) {
  58. // Section SERVICE ACTIVATED -------------------------------------------------
  59. section = [XLFormSectionDescriptor formSection];
  60. [form addFormSection:section];
  61. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"serviceActivated" rowType:XLFormRowDescriptorTypeInfo title:NSLocalizedString(@"_e2e_settings_activated_", nil)];
  62. row.cellConfigAtConfigure[@"backgroundColor"] = NCBrandColor.sharedInstance.backgroundView;
  63. [row.cellConfig setObject:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"selectFull"] width:50 height:50 color:[UIColor greenColor]] forKey:@"imageView.image"];
  64. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0] forKey:@"textLabel.font"];
  65. [row.cellConfig setObject:NCBrandColor.sharedInstance.textView forKey:@"textLabel.textColor"];
  66. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  67. [section addFormRow:row];
  68. // Section PASSPHRASE -------------------------------------------------
  69. section = [XLFormSectionDescriptor formSection];
  70. [form addFormSection:section];
  71. // Read Passphrase
  72. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"readPassphrase" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"_e2e_settings_read_passphrase_", nil)];
  73. row.cellConfigAtConfigure[@"backgroundColor"] = NCBrandColor.sharedInstance.backgroundView;
  74. [row.cellConfig setObject:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"e2eReadPassphrase"] width:50 height:50 color:NCBrandColor.sharedInstance.icon] forKey:@"imageView.image"];
  75. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0] forKey:@"textLabel.font"];
  76. [row.cellConfig setObject:NCBrandColor.sharedInstance.textView forKey:@"textLabel.textColor"];
  77. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  78. row.action.formSelector = @selector(readPassphrase:);
  79. [section addFormRow:row];
  80. // Section DELETE -------------------------------------------------
  81. section = [XLFormSectionDescriptor formSection];
  82. [form addFormSection:section];
  83. // remove locally Encryption
  84. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"removeLocallyEncryption" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"_e2e_settings_remove_", nil)];
  85. row.cellConfigAtConfigure[@"backgroundColor"] = NCBrandColor.sharedInstance.backgroundView;
  86. [row.cellConfig setObject:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"lock"] width:50 height:50 color:NCBrandColor.sharedInstance.icon] forKey:@"imageView.image"];
  87. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0] forKey:@"textLabel.font"];
  88. [row.cellConfig setObject:NCBrandColor.sharedInstance.textView forKey:@"textLabel.textColor"];
  89. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  90. row.action.formSelector = @selector(removeLocallyEncryption:);
  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.cellConfigAtConfigure[@"backgroundColor"] = NCBrandColor.sharedInstance.backgroundView;
  99. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0] forKey:@"textLabel.font"];
  100. [row.cellConfig setObject:NCBrandColor.sharedInstance.textView forKey:@"textLabel.textColor"];
  101. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  102. row.action.formSelector = @selector(startE2E:);
  103. [section addFormRow:row];
  104. }
  105. #ifdef DEBUG
  106. // Section DELETE KEYS -------------------------------------------------
  107. section = [XLFormSectionDescriptor formSectionWithTitle:NSLocalizedString(@"Delete server keys ", nil)];
  108. [form addFormSection:section];
  109. // Delete publicKey
  110. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"deletePublicKey" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"Delete PublicKey", nil)];
  111. row.cellConfigAtConfigure[@"backgroundColor"] = NCBrandColor.sharedInstance.backgroundView;
  112. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0] forKey:@"textLabel.font"];
  113. [row.cellConfig setObject:NCBrandColor.sharedInstance.textView forKey:@"textLabel.textColor"];
  114. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  115. row.action.formSelector = @selector(deletePublicKey:);
  116. [section addFormRow:row];
  117. // Delete privateKey
  118. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"deletePrivateKey" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"Delete PrivateKey", nil)];
  119. row.cellConfigAtConfigure[@"backgroundColor"] = NCBrandColor.sharedInstance.backgroundView;
  120. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0] forKey:@"textLabel.font"];
  121. [row.cellConfig setObject:NCBrandColor.sharedInstance.textView forKey:@"textLabel.textColor"];
  122. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  123. row.action.formSelector = @selector(deletePrivateKey:);
  124. [section addFormRow:row];
  125. #endif
  126. self.tableView.showsVerticalScrollIndicator = NO;
  127. self.form = form;
  128. }
  129. - (void)viewDidLoad
  130. {
  131. [super viewDidLoad];
  132. self.title = NSLocalizedString(@"_e2e_settings_", nil);
  133. appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  134. // E2EE
  135. self.endToEndInitialize = [NCEndToEndInitialize new];
  136. self.endToEndInitialize.delegate = self;
  137. // changeTheming
  138. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTheming) name:k_notificationCenter_changeTheming object:nil];
  139. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidEnterBackground) name:k_notificationCenter_applicationDidEnterBackground object:nil];
  140. [self changeTheming];
  141. }
  142. - (void)changeTheming
  143. {
  144. [appDelegate changeTheming:self tableView:self.tableView collectionView:nil form:true];
  145. [self initializeForm];
  146. }
  147. - (void)applicationDidEnterBackground
  148. {
  149. if (passcodeViewController.view.window != nil) {
  150. [passcodeViewController dismissViewControllerAnimated:true completion:nil];
  151. }
  152. }
  153. #pragma --------------------------------------------------------------------------------------------
  154. #pragma mark === Action ===
  155. #pragma --------------------------------------------------------------------------------------------
  156. - (void)startE2E:(XLFormRowDescriptor *)sender
  157. {
  158. [self deselectFormRow:sender];
  159. if ([[CCUtility getPasscode] length]) {
  160. [self passcodeType:@"startE2E"];
  161. } else {
  162. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_info_", nil) message:NSLocalizedString(@"_e2e_settings_lock_not_active_", nil) preferredStyle:UIAlertControllerStyleAlert];
  163. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"OK action") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  164. }];
  165. [alertController addAction:okAction];
  166. [self presentViewController:alertController animated:YES completion:nil];
  167. }
  168. }
  169. - (void)readPassphrase:(XLFormRowDescriptor *)sender
  170. {
  171. [self deselectFormRow:sender];
  172. if ([[CCUtility getPasscode] length]) {
  173. [self passcodeType:@"readPassphrase"];
  174. } else {
  175. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_info_", nil) message:NSLocalizedString(@"_e2e_settings_lock_not_active_", nil) preferredStyle:UIAlertControllerStyleAlert];
  176. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"OK action") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  177. }];
  178. [alertController addAction:okAction];
  179. [self presentViewController:alertController animated:YES completion:nil];
  180. }
  181. }
  182. - (void)removeLocallyEncryption:(XLFormRowDescriptor *)sender
  183. {
  184. [self deselectFormRow:sender];
  185. if ([[CCUtility getPasscode] length]) {
  186. [self passcodeType:@"removeLocallyEncryption"];
  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 mark - Passcode -
  196. - (void)passcodeType:(NSString *)type
  197. {
  198. LAContext *laContext = [LAContext new];
  199. NSError *error;
  200. if ([[CCUtility getPasscode] length] > 0) {
  201. passcodeViewController = [[TOPasscodeViewController alloc] initWithStyle:TOPasscodeViewStyleTranslucentLight passcodeType:TOPasscodeTypeSixDigits];
  202. if (@available(iOS 13.0, *)) {
  203. if ([[UITraitCollection currentTraitCollection] userInterfaceStyle] == UIUserInterfaceStyleDark) {
  204. passcodeViewController.style = TOPasscodeViewStyleTranslucentDark;
  205. }
  206. }
  207. passcodeViewController.delegate = self;
  208. passcodeViewController.allowCancel = true;
  209. passcodeViewController.keypadButtonShowLettering = false;
  210. if ([laContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
  211. if (error == NULL) {
  212. if (laContext.biometryType == LABiometryTypeFaceID) {
  213. passcodeViewController.biometryType = TOPasscodeBiometryTypeFaceID;
  214. passcodeViewController.allowBiometricValidation = true;
  215. passcodeViewController.automaticallyPromptForBiometricValidation = true;
  216. } else if (laContext.biometryType == LABiometryTypeTouchID) {
  217. passcodeViewController.biometryType = TOPasscodeBiometryTypeTouchID;
  218. passcodeViewController.allowBiometricValidation = true;
  219. passcodeViewController.automaticallyPromptForBiometricValidation = true;
  220. } else {
  221. NSLog(@"No Biometric support");
  222. }
  223. }
  224. }
  225. // Type of passcode
  226. passcodeType = type;
  227. [self presentViewController:passcodeViewController animated:YES completion:nil];
  228. }
  229. }
  230. - (void)didTapCancelInPasscodeViewController:(TOPasscodeViewController *)passcodeViewController
  231. {
  232. [passcodeViewController dismissViewControllerAnimated:YES completion:nil];
  233. }
  234. - (BOOL)passcodeViewController:(TOPasscodeViewController *)passcodeViewController isCorrectCode:(NSString *)code
  235. {
  236. if ([code isEqualToString:[CCUtility getPasscode]]) {
  237. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.5 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void) {
  238. [self passcodeCorrectCode];
  239. });
  240. return YES;
  241. }
  242. return NO;
  243. }
  244. - (void)didPerformBiometricValidationRequestInPasscodeViewController:(TOPasscodeViewController *)passcodeViewController
  245. {
  246. [[LAContext new] evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:[[NCBrandOptions sharedInstance] brand] reply:^(BOOL success, NSError * _Nullable error) {
  247. if (success) {
  248. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.5 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void) {
  249. [passcodeViewController dismissViewControllerAnimated:YES completion:nil];
  250. [self passcodeCorrectCode];
  251. });
  252. }
  253. }];
  254. }
  255. -(void)passcodeCorrectCode {
  256. if ([passcodeType isEqualToString:@"startE2E"]) {
  257. [self.endToEndInitialize initEndToEndEncryption];
  258. } else if ([passcodeType isEqualToString:@"readPassphrase"]) {
  259. NSString *e2ePassphrase = [CCUtility getEndToEndPassphrase:appDelegate.activeAccount];
  260. NSLog(@"[LOG] Passphrase: %@", e2ePassphrase);
  261. NSString *message = [NSString stringWithFormat:@"\n%@\n\n\n%@", NSLocalizedString(@"_e2e_settings_the_passphrase_is_", nil), e2ePassphrase];
  262. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_info_", nil) message:message preferredStyle:UIAlertControllerStyleAlert];
  263. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"OK action") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { }];
  264. [alertController addAction:okAction];
  265. [self presentViewController:alertController animated:YES completion:nil];
  266. } else if ([passcodeType isEqualToString:@"removeLocallyEncryption"]) {
  267. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_e2e_settings_remove_", nil) message:NSLocalizedString(@"_e2e_settings_remove_message_", nil) preferredStyle:UIAlertControllerStyleAlert];
  268. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"_remove_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  269. [CCUtility clearAllKeysEndToEnd:appDelegate.activeAccount];
  270. [self initializeForm];
  271. }];
  272. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_",nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {}];
  273. [alertController addAction:okAction];
  274. [alertController addAction:cancelAction];
  275. [self presentViewController:alertController animated:YES completion:nil];
  276. }
  277. }
  278. - (void)deletePublicKey:(XLFormRowDescriptor *)sender
  279. {
  280. [self deselectFormRow:sender];
  281. [[NCCommunication shared] deleteE2EEPublicKeyWithCustomUserAgent:nil addCustomHeaders:nil completionHandler:^(NSString *account, NSInteger errorCode, NSString *errorDescription) {
  282. if (errorCode == 0 && [account isEqualToString:appDelegate.activeAccount]) {
  283. [[NCContentPresenter shared] messageNotification:@"E2E delete publicKey" description:@"Success" delay:k_dismissAfterSecond type:messageTypeSuccess errorCode:0];
  284. } else {
  285. [[NCContentPresenter shared] messageNotification:@"E2E delete publicKey" description:errorDescription delay:k_dismissAfterSecond type:messageTypeError errorCode:errorCode];
  286. }
  287. }];
  288. }
  289. - (void)deletePrivateKey:(XLFormRowDescriptor *)sender
  290. {
  291. [self deselectFormRow:sender];
  292. [[NCCommunication shared] deleteE2EEPrivateKeyWithCustomUserAgent:nil addCustomHeaders:nil completionHandler:^(NSString *account, NSInteger errorCode, NSString *errorDescription) {
  293. if (errorCode == 0 && [account isEqualToString:appDelegate.activeAccount]) {
  294. [[NCContentPresenter shared] messageNotification:@"E2E delete privateKey" description:@"Success" delay:k_dismissAfterSecond type:messageTypeSuccess errorCode:0];
  295. } else {
  296. [[NCContentPresenter shared] messageNotification:@"E2E delete privateKey" description:errorDescription delay:k_dismissAfterSecond type:messageTypeError errorCode:errorCode];
  297. }
  298. }];
  299. }
  300. #pragma --------------------------------------------------------------------------------------------
  301. #pragma mark === Delegate ===
  302. #pragma --------------------------------------------------------------------------------------------
  303. - (void)endToEndInitializeSuccess
  304. {
  305. // Reload All Datasource
  306. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:k_notificationCenter_clearDateReadDataSource object:nil];
  307. [self initializeForm];
  308. }
  309. @end