NCManageEndToEndEncryption.m 19 KB

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