NCManageEndToEndEncryption.m 19 KB

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