NCManageEndToEndEncryption.m 20 KB

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