NCManageEndToEndEncryption.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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 "CCNetworking.h"
  26. #import <TOPasscodeViewController/TOPasscodeViewController.h>
  27. #import "NCBridgeSwift.h"
  28. @interface NCManageEndToEndEncryption () <NCEndToEndInitializeDelegate, TOPasscodeViewControllerDelegate>
  29. {
  30. AppDelegate *appDelegate;
  31. NSString *passcodeType;
  32. }
  33. @end
  34. @implementation NCManageEndToEndEncryption
  35. - (void)initializeForm
  36. {
  37. XLFormDescriptor *form = [XLFormDescriptor formDescriptorWithTitle:NSLocalizedString(@"_e2e_settings_", nil)];
  38. XLFormSectionDescriptor *section;
  39. XLFormRowDescriptor *row;
  40. tableCapabilities *capabilities = [[NCManageDatabase sharedInstance] getCapabilitesWithAccount:appDelegate.activeAccount];
  41. if (capabilities.endToEndEncryption == 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.backgroundView;
  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.backgroundView;
  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.backgroundView;
  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.backgroundView;
  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.backgroundView;
  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.backgroundView;
  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.backgroundView;
  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. appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  132. // E2EE
  133. self.endToEndInitialize = [NCEndToEndInitialize new];
  134. self.endToEndInitialize.delegate = self;
  135. // changeTheming
  136. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTheming) name:k_notificationCenter_changeTheming object:nil];
  137. [self changeTheming];
  138. }
  139. - (void)changeTheming
  140. {
  141. [appDelegate changeTheming:self tableView:self.tableView collectionView:nil form:true];
  142. [self initializeForm];
  143. }
  144. #pragma --------------------------------------------------------------------------------------------
  145. #pragma mark === Action ===
  146. #pragma --------------------------------------------------------------------------------------------
  147. - (void)startE2E:(XLFormRowDescriptor *)sender
  148. {
  149. [self deselectFormRow:sender];
  150. if ([[CCUtility getBlockCode] length]) {
  151. [self passcodeType:@"startE2E"];
  152. } else {
  153. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_info_", nil) message:NSLocalizedString(@"_e2e_settings_lock_not_active_", nil) preferredStyle:UIAlertControllerStyleAlert];
  154. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"OK action") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  155. }];
  156. [alertController addAction:okAction];
  157. [self presentViewController:alertController animated:YES completion:nil];
  158. }
  159. }
  160. - (void)readPassphrase:(XLFormRowDescriptor *)sender
  161. {
  162. [self deselectFormRow:sender];
  163. if ([[CCUtility getBlockCode] length]) {
  164. [self passcodeType:@"readPassphrase"];
  165. } else {
  166. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_info_", nil) message:NSLocalizedString(@"_e2e_settings_lock_not_active_", nil) preferredStyle:UIAlertControllerStyleAlert];
  167. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"OK action") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  168. }];
  169. [alertController addAction:okAction];
  170. [self presentViewController:alertController animated:YES completion:nil];
  171. }
  172. }
  173. - (void)removeLocallyEncryption:(XLFormRowDescriptor *)sender
  174. {
  175. [self deselectFormRow:sender];
  176. if ([[CCUtility getBlockCode] length]) {
  177. [self passcodeType:@"removeLocallyEncryption"];
  178. } else {
  179. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_info_", nil) message:NSLocalizedString(@"_e2e_settings_lock_not_active_", nil) preferredStyle:UIAlertControllerStyleAlert];
  180. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"OK action") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  181. }];
  182. [alertController addAction:okAction];
  183. [self presentViewController:alertController animated:YES completion:nil];
  184. }
  185. }
  186. #pragma mark - Passcode -
  187. - (void)passcodeType:(NSString *)type
  188. {
  189. LAContext *laContext = [LAContext new];
  190. NSError *error;
  191. if ([[CCUtility getBlockCode] length] > 0) {
  192. TOPasscodeViewController *passcodeViewController = [[TOPasscodeViewController alloc] initWithStyle:TOPasscodeViewStyleTranslucentLight passcodeType:TOPasscodeTypeSixDigits];
  193. if (@available(iOS 13.0, *)) {
  194. if ([[UITraitCollection currentTraitCollection] userInterfaceStyle] == UIUserInterfaceStyleDark) {
  195. passcodeViewController.style = TOPasscodeViewStyleTranslucentDark;
  196. }
  197. }
  198. passcodeViewController.delegate = self;
  199. passcodeViewController.allowCancel = false;
  200. passcodeViewController.keypadButtonShowLettering = false;
  201. if ([laContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
  202. if (error == NULL) {
  203. if (laContext.biometryType == LABiometryTypeFaceID) {
  204. passcodeViewController.biometryType = TOPasscodeBiometryTypeFaceID;
  205. passcodeViewController.allowBiometricValidation = true;
  206. passcodeViewController.automaticallyPromptForBiometricValidation = true;
  207. } else if (laContext.biometryType == LABiometryTypeTouchID) {
  208. passcodeViewController.biometryType = TOPasscodeBiometryTypeTouchID;
  209. passcodeViewController.allowBiometricValidation = true;
  210. passcodeViewController.automaticallyPromptForBiometricValidation = true;
  211. } else {
  212. NSLog(@"No Biometric support");
  213. }
  214. }
  215. }
  216. [self presentViewController:passcodeViewController animated:YES completion:nil];
  217. }
  218. }
  219. - (void)didTapCancelInPasscodeViewController:(TOPasscodeViewController *)passcodeViewController
  220. {
  221. [passcodeViewController dismissViewControllerAnimated:YES completion:nil];
  222. }
  223. - (BOOL)passcodeViewController:(TOPasscodeViewController *)passcodeViewController isCorrectCode:(NSString *)code
  224. {
  225. if ([code isEqualToString:[CCUtility getBlockCode]]) {
  226. [self passcodeCorrectCode];
  227. return YES;
  228. }
  229. return NO;
  230. }
  231. - (void)didPerformBiometricValidationRequestInPasscodeViewController:(TOPasscodeViewController *)passcodeViewController
  232. {
  233. [[LAContext new] evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:[[NCBrandOptions sharedInstance] brand] reply:^(BOOL success, NSError * _Nullable error) {
  234. if (success) {
  235. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.5 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void) {
  236. [self passcodeCorrectCode];
  237. });
  238. }
  239. }];
  240. }
  241. -(void)passcodeCorrectCode {
  242. if ([passcodeType isEqualToString:@"startE2E"]) {
  243. [self.endToEndInitialize initEndToEndEncryption];
  244. } else if ([passcodeType isEqualToString:@"readPassphrase"]) {
  245. NSString *e2ePassphrase = [CCUtility getEndToEndPassphrase:appDelegate.activeAccount];
  246. NSLog(@"[LOG] Passphrase: %@", e2ePassphrase);
  247. NSString *message = [NSString stringWithFormat:@"\n%@\n\n\n%@", NSLocalizedString(@"_e2e_settings_the_passphrase_is_", nil), e2ePassphrase];
  248. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_info_", nil) message:message preferredStyle:UIAlertControllerStyleAlert];
  249. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"OK action") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { }];
  250. [alertController addAction:okAction];
  251. [self presentViewController:alertController animated:YES completion:nil];
  252. } else if ([passcodeType isEqualToString:@"removeLocallyEncryption"]) {
  253. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_e2e_settings_remove_", nil) message:NSLocalizedString(@"_e2e_settings_remove_message_", nil) preferredStyle:UIAlertControllerStyleAlert];
  254. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"_remove_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  255. [CCUtility clearAllKeysEndToEnd:appDelegate.activeAccount];
  256. [self initializeForm];
  257. }];
  258. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_",nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {}];
  259. [alertController addAction:okAction];
  260. [alertController addAction:cancelAction];
  261. [self presentViewController:alertController animated:YES completion:nil];
  262. }
  263. }
  264. - (void)deletePublicKey:(XLFormRowDescriptor *)sender
  265. {
  266. [self deselectFormRow:sender];
  267. [[NCNetworkingEndToEnd sharedManager] deleteEndToEndPublicKeyWithAccount:appDelegate.activeAccount completion:^(NSString *account, NSString *message, NSInteger errorCode) {
  268. if (errorCode == 0 && [account isEqualToString:appDelegate.activeAccount]) {
  269. [[NCContentPresenter shared] messageNotification:@"E2E delete publicKey" description:@"Success" delay:k_dismissAfterSecond type:messageTypeSuccess errorCode:0];
  270. } else {
  271. [[NCContentPresenter shared] messageNotification:@"E2E delete publicKey" description:message delay:k_dismissAfterSecond type:messageTypeError errorCode:k_CCErrorInternalError];
  272. }
  273. }];
  274. }
  275. - (void)deletePrivateKey:(XLFormRowDescriptor *)sender
  276. {
  277. [self deselectFormRow:sender];
  278. [[NCNetworkingEndToEnd sharedManager] deleteEndToEndPrivateKeyWithAccount:appDelegate.activeAccount completion:^(NSString *account, NSString *message, NSInteger errorCode) {
  279. if (errorCode == 0 && [account isEqualToString:appDelegate.activeAccount]) {
  280. [[NCContentPresenter shared] messageNotification:@"E2E delete privateKey" description:@"Success" delay:k_dismissAfterSecond type:messageTypeSuccess errorCode:0];
  281. } else {
  282. [[NCContentPresenter shared] messageNotification:@"E2E delete privateKey" description:message delay:k_dismissAfterSecond type:messageTypeError errorCode:k_CCErrorInternalError];
  283. }
  284. }];
  285. }
  286. #pragma --------------------------------------------------------------------------------------------
  287. #pragma mark === Delegate ===
  288. #pragma --------------------------------------------------------------------------------------------
  289. - (void)endToEndInitializeSuccess
  290. {
  291. // Reload All Datasource
  292. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:k_notificationCenter_clearDateReadDataSource object:nil];
  293. [self initializeForm];
  294. }
  295. @end