NCManageEndToEndEncryption.m 20 KB

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