NCManageEndToEndEncryption.m 20 KB

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