NCManageEndToEndEncryption.m 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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 "NSNotificationCenter+MainThread.h"
  25. #import <LocalAuthentication/LocalAuthentication.h>
  26. #import <TOPasscodeViewController/TOPasscodeViewController.h>
  27. #import "NCBridgeSwift.h"
  28. #import "CCUtility.h"
  29. @interface NCManageEndToEndEncryption () <NCEndToEndInitializeDelegate, TOPasscodeViewControllerDelegate>
  30. {
  31. AppDelegate *appDelegate;
  32. NSString *passcodeType;
  33. TOPasscodeViewController *passcodeViewController;
  34. }
  35. @end
  36. @implementation NCManageEndToEndEncryption
  37. - (void)initializeForm
  38. {
  39. XLFormDescriptor *form = [XLFormDescriptor formDescriptor];
  40. XLFormSectionDescriptor *section;
  41. XLFormRowDescriptor *row;
  42. BOOL isE2EEEnabled = [[NCManageDatabase shared] getCapabilitiesServerBoolWithAccount:appDelegate.account elements:NCElementsJSON.shared.capabilitiesE2EEEnabled exists:false];
  43. NSString *versionE2EE = [[NCManageDatabase shared] getCapabilitiesServerStringWithAccount:appDelegate.account elements:NCElementsJSON.shared.capabilitiesE2EEApiVersion];
  44. if (![versionE2EE isEqual:[[NCGlobal shared] e2eeVersion]] && isE2EEEnabled) {
  45. NKError *error = [[NKError alloc] initWithErrorCode:NCGlobal.shared.errorInternalError errorDescription:@"_err_e2ee_app_version_"];
  46. [[NCContentPresenter shared] messageNotification:@"_error_e2ee_" error:error delay:[[NCGlobal shared] dismissAfterSecond] type:messageTypeError];
  47. }
  48. if (isE2EEEnabled == NO || ![versionE2EE isEqual:[[NCGlobal shared] e2eeVersion]]) {
  49. // Section SERVICE NOT AVAILABLE -------------------------------------------------
  50. section = [XLFormSectionDescriptor formSection];
  51. if (isE2EEEnabled) {
  52. section.footerTitle = [NSString stringWithFormat:@"End-to-End Encryption %@", versionE2EE];
  53. }
  54. [form addFormSection:section];
  55. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"serviceActivated" rowType:XLFormRowDescriptorTypeInfo title:NSLocalizedString(@"_e2e_settings_not_available_", nil)];
  56. row.cellConfigAtConfigure[@"backgroundColor"] = UIColor.secondarySystemGroupedBackgroundColor;
  57. [row.cellConfig setObject:[[UIImage imageNamed:@"closeCircle"] imageWithColor:[UIColor redColor] size:25] forKey:@"imageView.image"];
  58. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0] forKey:@"textLabel.font"];
  59. [row.cellConfig setObject:UIColor.labelColor forKey:@"textLabel.textColor"];
  60. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  61. [section addFormRow:row];
  62. self.tableView.showsVerticalScrollIndicator = NO;
  63. self.form = form;
  64. return;
  65. }
  66. if ([CCUtility isEndToEndEnabled:appDelegate.account]) {
  67. // Section SERVICE ACTIVATED -------------------------------------------------
  68. section = [XLFormSectionDescriptor formSection];
  69. section.footerTitle = [NSString stringWithFormat:@"End-to-End Encryption %@", versionE2EE];
  70. [form addFormSection:section];
  71. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"serviceActivated" rowType:XLFormRowDescriptorTypeInfo title:NSLocalizedString(@"_e2e_settings_activated_", nil)];
  72. row.cellConfigAtConfigure[@"backgroundColor"] = UIColor.secondarySystemGroupedBackgroundColor;
  73. [row.cellConfig setObject:[[UIImage imageNamed:@"checkmark.circle.fill"] imageWithColor:[UIColor greenColor] size:25] forKey:@"imageView.image"];
  74. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0] forKey:@"textLabel.font"];
  75. [row.cellConfig setObject:UIColor.labelColor forKey:@"textLabel.textColor"];
  76. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  77. [section addFormRow:row];
  78. // Section PASSPHRASE -------------------------------------------------
  79. section = [XLFormSectionDescriptor formSection];
  80. [form addFormSection:section];
  81. // Read Passphrase
  82. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"readPassphrase" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"_e2e_settings_read_passphrase_", nil)];
  83. row.cellConfigAtConfigure[@"backgroundColor"] = UIColor.secondarySystemGroupedBackgroundColor;
  84. [row.cellConfig setObject:[[UIImage imageNamed:@"e2eReadPassphrase"] imageWithColor:NCBrandColor.shared.gray size:25] forKey:@"imageView.image"];
  85. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0] forKey:@"textLabel.font"];
  86. [row.cellConfig setObject:UIColor.labelColor forKey:@"textLabel.textColor"];
  87. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  88. row.action.formSelector = @selector(readPassphrase:);
  89. [section addFormRow:row];
  90. // Section DELETE -------------------------------------------------
  91. section = [XLFormSectionDescriptor formSection];
  92. [form addFormSection:section];
  93. // remove locally Encryption
  94. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"removeLocallyEncryption" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"_e2e_settings_remove_", nil)];
  95. row.cellConfigAtConfigure[@"backgroundColor"] = UIColor.secondarySystemGroupedBackgroundColor;
  96. [row.cellConfig setObject:[[UIImage imageNamed:@"lock"] imageWithColor:NCBrandColor.shared.gray size:25] forKey:@"imageView.image"];
  97. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0] forKey:@"textLabel.font"];
  98. [row.cellConfig setObject:UIColor.labelColor forKey:@"textLabel.textColor"];
  99. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  100. row.action.formSelector = @selector(removeLocallyEncryption:);
  101. [section addFormRow:row];
  102. } else {
  103. // Section START E2E -------------------------------------------------
  104. section = [XLFormSectionDescriptor formSection];
  105. [form addFormSection:section];
  106. // Start e2e
  107. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"startE2E" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"_e2e_settings_start_", nil)];
  108. row.cellConfigAtConfigure[@"backgroundColor"] = UIColor.secondarySystemGroupedBackgroundColor;
  109. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0] forKey:@"textLabel.font"];
  110. [row.cellConfig setObject:UIColor.labelColor forKey:@"textLabel.textColor"];
  111. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  112. row.action.formSelector = @selector(startE2E:);
  113. [section addFormRow:row];
  114. }
  115. #ifdef DEBUG
  116. // Section DELETE KEYS -------------------------------------------------
  117. section = [XLFormSectionDescriptor formSectionWithTitle:NSLocalizedString(@"Delete server keys ", nil)];
  118. [form addFormSection:section];
  119. // Delete publicKey
  120. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"deleteCertificate" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"Delete certificate", nil)];
  121. row.cellConfigAtConfigure[@"backgroundColor"] = UIColor.secondarySystemGroupedBackgroundColor;
  122. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0] forKey:@"textLabel.font"];
  123. [row.cellConfig setObject:UIColor.labelColor forKey:@"textLabel.textColor"];
  124. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  125. row.action.formSelector = @selector(deleteCertificate:);
  126. [section addFormRow:row];
  127. // Delete privateKey
  128. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"deletePrivateKey" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"Delete PrivateKey", nil)];
  129. row.cellConfigAtConfigure[@"backgroundColor"] = UIColor.secondarySystemGroupedBackgroundColor;
  130. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0] forKey:@"textLabel.font"];
  131. [row.cellConfig setObject:UIColor.labelColor 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.tableView.showsVerticalScrollIndicator = NO;
  137. self.form = form;
  138. }
  139. // MARK: - View Life Cycle
  140. - (void)viewDidLoad
  141. {
  142. [super viewDidLoad];
  143. self.title = NSLocalizedString(@"_e2e_settings_", nil);
  144. appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  145. self.view.backgroundColor = UIColor.systemGroupedBackgroundColor;
  146. self.tableView.backgroundColor = UIColor.systemGroupedBackgroundColor;
  147. // E2EE
  148. self.endToEndInitialize = [NCEndToEndInitialize new];
  149. self.endToEndInitialize.delegate = self;
  150. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidEnterBackground) name:NCGlobal.shared.notificationCenterApplicationDidEnterBackground object:nil];
  151. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(initialize) name:NCGlobal.shared.notificationCenterInitialize object:nil];
  152. [self initializeForm];
  153. /*
  154. NKRequestOptions *options = [[NKRequestOptions alloc] initWithEndpoint:nil customHeader:nil customUserAgent:nil contentType:nil e2eToken: nil timeout:30 queue:dispatch_get_main_queue()];
  155. [[NextcloudKit shared] getE2EECertificateWithOptions:options completionHandler:^(NSString* account, NSString *certificate, NSData *data, NKError *error) {
  156. }];
  157. */
  158. }
  159. - (void)viewWillAppear:(BOOL)animated
  160. {
  161. [super viewWillAppear:animated];
  162. appDelegate.activeViewController = self;
  163. }
  164. #pragma mark - NotificationCenter
  165. - (void)applicationDidEnterBackground
  166. {
  167. if (passcodeViewController.view.window != nil) {
  168. [passcodeViewController dismissViewControllerAnimated:true completion:nil];
  169. }
  170. }
  171. - (void)initialize
  172. {
  173. [[self navigationController] popViewControllerAnimated:YES];
  174. }
  175. #pragma mark - Action
  176. - (void)startE2E:(XLFormRowDescriptor *)sender
  177. {
  178. [self deselectFormRow:sender];
  179. if ([[CCUtility getPasscode] length]) {
  180. [self passcodeType:@"startE2E"];
  181. } else {
  182. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_info_", nil) message:NSLocalizedString(@"_e2e_settings_lock_not_active_", nil) preferredStyle:UIAlertControllerStyleAlert];
  183. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"OK action") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  184. }];
  185. [alertController addAction:okAction];
  186. [self presentViewController:alertController animated:YES completion:nil];
  187. }
  188. }
  189. - (void)readPassphrase:(XLFormRowDescriptor *)sender
  190. {
  191. [self deselectFormRow:sender];
  192. if ([[CCUtility getPasscode] length]) {
  193. [self passcodeType:@"readPassphrase"];
  194. } else {
  195. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_info_", nil) message:NSLocalizedString(@"_e2e_settings_lock_not_active_", nil) preferredStyle:UIAlertControllerStyleAlert];
  196. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"OK action") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  197. }];
  198. [alertController addAction:okAction];
  199. [self presentViewController:alertController animated:YES completion:nil];
  200. }
  201. }
  202. - (void)removeLocallyEncryption:(XLFormRowDescriptor *)sender
  203. {
  204. [self deselectFormRow:sender];
  205. if ([[CCUtility getPasscode] length]) {
  206. [self passcodeType:@"removeLocallyEncryption"];
  207. } else {
  208. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_info_", nil) message:NSLocalizedString(@"_e2e_settings_lock_not_active_", nil) preferredStyle:UIAlertControllerStyleAlert];
  209. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"OK action") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  210. }];
  211. [alertController addAction:okAction];
  212. [self presentViewController:alertController animated:YES completion:nil];
  213. }
  214. }
  215. #pragma mark - Passcode -
  216. - (void)passcodeType:(NSString *)type
  217. {
  218. LAContext *laContext = [LAContext new];
  219. NSError *error;
  220. if ([[CCUtility getPasscode] length] > 0) {
  221. passcodeViewController = [[TOPasscodeViewController alloc] initPasscodeType:TOPasscodeTypeSixDigits allowCancel:true];
  222. passcodeViewController.delegate = self;
  223. passcodeViewController.keypadButtonShowLettering = false;
  224. if (CCUtility.getEnableTouchFaceID && [laContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
  225. if (error == NULL) {
  226. if (laContext.biometryType == LABiometryTypeFaceID) {
  227. passcodeViewController.biometryType = TOPasscodeBiometryTypeFaceID;
  228. passcodeViewController.allowBiometricValidation = true;
  229. passcodeViewController.automaticallyPromptForBiometricValidation = true;
  230. } else if (laContext.biometryType == LABiometryTypeTouchID) {
  231. passcodeViewController.biometryType = TOPasscodeBiometryTypeTouchID;
  232. passcodeViewController.allowBiometricValidation = true;
  233. passcodeViewController.automaticallyPromptForBiometricValidation = true;
  234. } else {
  235. NSLog(@"No Biometric support");
  236. }
  237. }
  238. }
  239. // Type of passcode
  240. passcodeType = type;
  241. [self presentViewController:passcodeViewController animated:YES completion:nil];
  242. }
  243. }
  244. - (void)didTapCancelInPasscodeViewController:(TOPasscodeViewController *)passcodeViewController
  245. {
  246. [passcodeViewController dismissViewControllerAnimated:YES completion:nil];
  247. }
  248. - (BOOL)passcodeViewController:(TOPasscodeViewController *)passcodeViewController isCorrectCode:(NSString *)code
  249. {
  250. if ([code isEqualToString:[CCUtility getPasscode]]) {
  251. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.5 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void) {
  252. [self passcodeCorrectCode];
  253. });
  254. return YES;
  255. }
  256. return NO;
  257. }
  258. - (void)didPerformBiometricValidationRequestInPasscodeViewController:(TOPasscodeViewController *)passcodeViewController
  259. {
  260. [[LAContext new] evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:[[NCBrandOptions shared] brand] reply:^(BOOL success, NSError * _Nullable error) {
  261. if (success) {
  262. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.5 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void) {
  263. [passcodeViewController dismissViewControllerAnimated:YES completion:nil];
  264. [self passcodeCorrectCode];
  265. });
  266. }
  267. }];
  268. }
  269. -(void)passcodeCorrectCode {
  270. if ([passcodeType isEqualToString:@"startE2E"]) {
  271. [self.endToEndInitialize initEndToEndEncryption];
  272. } else if ([passcodeType isEqualToString:@"readPassphrase"]) {
  273. NSString *e2ePassphrase = [CCUtility getEndToEndPassphrase:appDelegate.account];
  274. NSLog(@"[LOG] Passphrase: %@", e2ePassphrase);
  275. NSString *message = [NSString stringWithFormat:@"\n%@\n\n\n%@", NSLocalizedString(@"_e2e_settings_the_passphrase_is_", nil), e2ePassphrase];
  276. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_info_", nil) message:message preferredStyle:UIAlertControllerStyleAlert];
  277. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { }];
  278. UIAlertAction *copyPassphrase = [UIAlertAction actionWithTitle:NSLocalizedString(@"_copy_passphrase_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  279. UIPasteboard.generalPasteboard.string = e2ePassphrase;
  280. }];
  281. [alertController addAction:okAction];
  282. [alertController addAction:copyPassphrase];
  283. [self presentViewController:alertController animated:YES completion:nil];
  284. } else if ([passcodeType isEqualToString:@"removeLocallyEncryption"]) {
  285. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_e2e_settings_remove_", nil) message:NSLocalizedString(@"_e2e_settings_remove_message_", nil) preferredStyle:UIAlertControllerStyleAlert];
  286. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"_remove_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  287. [CCUtility clearAllKeysEndToEnd:appDelegate.account];
  288. [self initializeForm];
  289. }];
  290. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_",nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {}];
  291. [alertController addAction:okAction];
  292. [alertController addAction:cancelAction];
  293. [self presentViewController:alertController animated:YES completion:nil];
  294. }
  295. }
  296. - (void)deleteCertificate:(XLFormRowDescriptor *)sender
  297. {
  298. [self deselectFormRow:sender];
  299. NKRequestOptions *options = [[NKRequestOptions alloc] initWithEndpoint:nil customHeader:nil customUserAgent:nil contentType:nil e2eToken: nil timeout:60 queue:dispatch_get_main_queue()];
  300. [[NextcloudKit shared] deleteE2EECertificateWithOptions:options completionHandler:^(NSString *account, NKError *error) {
  301. if (error == NKError.success && [account isEqualToString:appDelegate.account]) {
  302. NKError *error = [[NKError alloc] initWithErrorCode:NCGlobal.shared.errorInternalError errorDescription:@"Success"];
  303. [[NCContentPresenter shared] messageNotification:@"E2E delete certificate" error:error delay:[[NCGlobal shared] dismissAfterSecond] type:messageTypeSuccess];
  304. } else {
  305. [[NCContentPresenter shared] messageNotification:@"E2E delete certificate" error:error delay:[[NCGlobal shared] dismissAfterSecond] type:messageTypeError];
  306. }
  307. }];
  308. }
  309. - (void)deletePrivateKey:(XLFormRowDescriptor *)sender
  310. {
  311. [self deselectFormRow:sender];
  312. NKRequestOptions *options = [[NKRequestOptions alloc] initWithEndpoint:nil customHeader:nil customUserAgent:nil contentType:nil e2eToken: nil timeout:60 queue:dispatch_get_main_queue()];
  313. [[NextcloudKit shared] deleteE2EEPrivateKeyWithOptions: options completionHandler:^(NSString *account, NKError *error) {
  314. if (error == NKError.success && [account isEqualToString:appDelegate.account]) {
  315. NKError *error = [[NKError alloc] initWithErrorCode:NCGlobal.shared.errorInternalError errorDescription:@"Success"];
  316. [[NCContentPresenter shared] messageNotification:@"E2E delete privateKey" error:error delay:[[NCGlobal shared] dismissAfterSecond] type:messageTypeSuccess];
  317. } else {
  318. [[NCContentPresenter shared] messageNotification:@"E2E delete privateKey" error:error delay:[[NCGlobal shared] dismissAfterSecond] type:messageTypeError];
  319. }
  320. }];
  321. }
  322. #pragma mark - Delegate
  323. - (void)endToEndInitializeSuccess
  324. {
  325. // Reload All Datasource
  326. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:NCGlobal.shared.notificationCenterReloadDataSource object:nil];
  327. [self initializeForm];
  328. }
  329. #pragma mark -
  330. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  331. return NCGlobal.shared.heightCellSettings;
  332. }
  333. @end