NCNetworkingEndToEnd.m 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  1. //
  2. // NCNetworkingEndToEnd.m
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 29/10/17.
  6. // Copyright (c) 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 "NCNetworkingEndToEnd.h"
  24. #import "OCNetworking.h"
  25. #import "CCUtility.h"
  26. #import "NCBridgeSwift.h"
  27. /*********************************************************************************
  28. Netwok call synchronous mode, use this only from :
  29. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  30. });
  31. *********************************************************************************/
  32. @implementation NCNetworkingEndToEnd
  33. + (NCNetworkingEndToEnd *)sharedManager {
  34. static NCNetworkingEndToEnd *sharedManager;
  35. @synchronized(self)
  36. {
  37. if (!sharedManager) {
  38. sharedManager = [NCNetworkingEndToEnd new];
  39. }
  40. return sharedManager;
  41. }
  42. }
  43. #pragma --------------------------------------------------------------------------------------------
  44. #pragma mark ===== End-to-End Encryption NETWORKING =====
  45. #pragma --------------------------------------------------------------------------------------------
  46. - (void)getEndToEndPublicKeyWithAccount:(NSString *)account completion:(void (^)(NSString *account, NSString *publicKey, NSString *message, NSInteger errorCode))completion
  47. {
  48. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  49. if (tableAccount == nil) {
  50. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  51. }
  52. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  53. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  54. [communication setUserAgent:[CCUtility getUserAgent]];
  55. [communication getEndToEndPublicKeys:[tableAccount.url stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *publicKey, NSString *redirectedServer) {
  56. completion(account, publicKey, nil, 0);
  57. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  58. NSString *message = @"";
  59. NSInteger errorCode = response.statusCode;
  60. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  61. errorCode = error.code;
  62. // Error
  63. if (errorCode == 503) {
  64. message = NSLocalizedString(@"_server_error_retry_", nil);
  65. } else {
  66. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  67. }
  68. completion(account, nil, message, errorCode);
  69. }];
  70. }
  71. - (void)getEndToEndPrivateKeyCipherWithAccount:(NSString *)account completion:(void (^)(NSString *account, NSString *privateKeyChiper, NSString *message, NSInteger errorCode))completion
  72. {
  73. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  74. if (tableAccount == nil) {
  75. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  76. }
  77. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  78. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  79. [communication setUserAgent:[CCUtility getUserAgent]];
  80. [communication getEndToEndPrivateKeyCipher:[tableAccount.url stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *privateKeyChiper, NSString *redirectedServer) {
  81. completion(account, privateKeyChiper, nil, 0);
  82. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  83. NSString *message = @"";
  84. NSInteger errorCode = response.statusCode;
  85. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  86. errorCode = error.code;
  87. // Error
  88. if (errorCode == 503) {
  89. message = NSLocalizedString(@"_server_error_retry_", nil);
  90. } else {
  91. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  92. }
  93. completion(account, nil, message, errorCode);
  94. }];
  95. }
  96. - (void)signEndToEndPublicKeyWithAccount:(NSString *)account publicKey:(NSString *)publicKey completion:(void (^)(NSString *account, NSString *publicKey, NSString *message, NSInteger errorCode))completion
  97. {
  98. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  99. if (tableAccount == nil) {
  100. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  101. }
  102. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  103. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  104. [communication setUserAgent:[CCUtility getUserAgent]];
  105. [communication signEndToEndPublicKey:[tableAccount.url stringByAppendingString:@"/"] publicKey:[CCUtility URLEncodeStringFromString:publicKey] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *publicKey, NSString *redirectedServer) {
  106. completion(account, publicKey, nil, 0);
  107. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  108. NSString *message = @"";
  109. NSInteger errorCode = response.statusCode;
  110. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  111. errorCode = error.code;
  112. // Error
  113. if (errorCode == 503) {
  114. message = NSLocalizedString(@"_server_error_retry_", nil);
  115. } else {
  116. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  117. }
  118. completion(account, nil, message, errorCode);
  119. }];
  120. }
  121. - (void)storeEndToEndPrivateKeyCipherWithAccount:(NSString *)account privateKeyString:(NSString *)privateKeyString privateKeyChiper:(NSString *)privateKeyChiper completion:(void (^)(NSString *account, NSString *privateKeyString, NSString *privateKey, NSString *message, NSInteger errorCode))completion
  122. {
  123. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  124. if (tableAccount == nil) {
  125. completion(account, nil, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  126. }
  127. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  128. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  129. [communication setUserAgent:[CCUtility getUserAgent]];
  130. [communication storeEndToEndPrivateKeyCipher:[tableAccount.url stringByAppendingString:@"/"] privateKeyChiper:[CCUtility URLEncodeStringFromString:privateKeyChiper] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *privateKey, NSString *redirectedServer) {
  131. completion(account, privateKeyString, privateKeyChiper, nil, 0);
  132. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  133. NSString *message = @"";
  134. NSInteger errorCode = response.statusCode;
  135. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  136. errorCode = error.code;
  137. // Error
  138. if (errorCode == 503) {
  139. message = NSLocalizedString(@"_server_error_retry_", nil);
  140. } else {
  141. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  142. }
  143. completion(account, nil, nil, message, errorCode);
  144. }];
  145. }
  146. - (void)deleteEndToEndPublicKeyWithAccount:(NSString *)account completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  147. {
  148. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  149. if (tableAccount == nil) {
  150. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  151. }
  152. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  153. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  154. [communication setUserAgent:[CCUtility getUserAgent]];
  155. [communication deleteEndToEndPublicKey:[tableAccount.url stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  156. completion(account, nil ,0);
  157. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  158. NSString *message = @"";
  159. NSInteger errorCode = response.statusCode;
  160. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  161. errorCode = error.code;
  162. // Error
  163. if (errorCode == 503) {
  164. message = NSLocalizedString(@"_server_error_retry_", nil);
  165. } else {
  166. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  167. }
  168. completion(account, message, errorCode);
  169. }];
  170. }
  171. - (void)deleteEndToEndPrivateKeyWithAccount:(NSString *)account completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  172. {
  173. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  174. if (tableAccount == nil) {
  175. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  176. }
  177. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  178. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  179. [communication setUserAgent:[CCUtility getUserAgent]];
  180. [communication deleteEndToEndPrivateKey:[tableAccount.url stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  181. completion(account, nil, 0);
  182. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  183. NSString *message = @"";
  184. NSInteger errorCode = response.statusCode;
  185. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  186. errorCode = error.code;
  187. // Error
  188. if (errorCode == 503) {
  189. message = NSLocalizedString(@"_server_error_retry_", nil);
  190. } else {
  191. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  192. }
  193. completion(account, message, errorCode);
  194. }];
  195. }
  196. - (void)getEndToEndServerPublicKeyWithAccount:(NSString *)account completion:(void (^)(NSString *account, NSString *publicKey, NSString *message, NSInteger errorCode))completion
  197. {
  198. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  199. if (tableAccount == nil) {
  200. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  201. }
  202. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  203. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:[CCUtility getPassword:account]];
  204. [communication setUserAgent:[CCUtility getUserAgent]];
  205. [communication getEndToEndServerPublicKey:[tableAccount.url stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *publicKey, NSString *redirectedServer) {
  206. completion(account, publicKey, nil, 0);
  207. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  208. NSString *message = @"";
  209. NSInteger errorCode = response.statusCode;
  210. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  211. errorCode = error.code;
  212. // Error
  213. if (errorCode == 503) {
  214. message = NSLocalizedString(@"_server_error_retry_", nil);
  215. } else {
  216. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  217. }
  218. completion(account, nil, message, errorCode);
  219. }];
  220. }
  221. - (void)createEndToEndFolder:(NSString *)folderPathName account:(NSString *)account user:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url encrypted:(BOOL)encrypted ocId:(NSString **)ocId error:(NSError **)error
  222. {
  223. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  224. __block NSError *returnError = nil;
  225. __block NSString *returnocId = nil;
  226. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  227. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  228. [communication setUserAgent:[CCUtility getUserAgent]];
  229. [communication readFile:folderPathName onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer) {
  230. dispatch_semaphore_signal(semaphore);
  231. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  232. [communication createFolder:folderPathName onCommunication:communication withForbiddenCharactersSupported:YES successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  233. NSDictionary *fields = [response allHeaderFields];
  234. returnocId = [CCUtility removeForbiddenCharactersFileSystem:[fields objectForKey:@"OC-FileId"]];
  235. if (encrypted) {
  236. // MARK
  237. [communication markEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] ocId:returnocId onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  238. [[NCManageDatabase sharedInstance] clearDateReadWithServerUrl:[CCUtility deletingLastPathComponentFromServerUrl:folderPathName] account:account];
  239. dispatch_semaphore_signal(semaphore);
  240. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  241. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_mark_folder_"];
  242. dispatch_semaphore_signal(semaphore);
  243. }];
  244. } else {
  245. [[NCManageDatabase sharedInstance] clearDateReadWithServerUrl:[CCUtility deletingLastPathComponentFromServerUrl:folderPathName] account:account];
  246. dispatch_semaphore_signal(semaphore);
  247. }
  248. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  249. returnError = [self getError:response error:error descriptionDefault:@"_error_"];
  250. dispatch_semaphore_signal(semaphore);
  251. } errorBeforeRequest:^(NSError *error) {
  252. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:response.description forKey:NSLocalizedDescriptionKey]];
  253. dispatch_semaphore_signal(semaphore);
  254. }];
  255. }];
  256. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  257. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  258. *ocId = returnocId;
  259. *error = returnError;
  260. }
  261. #pragma --------------------------------------------------------------------------------------------
  262. #pragma mark ===== E2EE End-to-End Encryption =====
  263. #pragma --------------------------------------------------------------------------------------------
  264. // E2EE
  265. - (NSError *)markEndToEndFolderEncryptedOnServerUrl:(NSString *)serverUrl ocId:(NSString *)ocId user:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url
  266. {
  267. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  268. __block NSError *returnError = nil;
  269. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  270. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  271. [communication setUserAgent:[CCUtility getUserAgent]];
  272. tableE2eEncryptionLock *tableLock = [[NCManageDatabase sharedInstance] getE2ETokenLockWithServerUrl:serverUrl];
  273. // Read Folder
  274. [communication readFolder:serverUrl depth:@"1" withUserSessionToken:nil onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer, NSString *tokenReadFolder) {
  275. if (items.count > 1) {
  276. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:999 userInfo:[NSDictionary dictionaryWithObject:NSLocalizedString(@"_e2e_error_directory_not_empty_", nil) forKey:NSLocalizedDescriptionKey]];
  277. dispatch_semaphore_signal(semaphore);
  278. return;
  279. }
  280. // LOCK
  281. [communication lockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] ocId:ocId token:tableLock.token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer) {
  282. [[NCManageDatabase sharedInstance] setE2ETokenLockWithServerUrl:serverUrl ocId:ocId token:token];
  283. // REMOVE METADATA
  284. [communication deleteEndToEndMetadata:[url stringByAppendingString:@"/"] ocId:ocId onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  285. NSLog(@"[LOG] Found metadata and delete");
  286. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  287. NSLog(@"[LOG] %@", [NSString stringWithFormat:@"Remove metadata error %d", (int)response.statusCode]);
  288. }];
  289. // MARK
  290. [communication markEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] ocId:ocId onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  291. // UNLOCK
  292. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] ocId:ocId token:token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  293. [[NCManageDatabase sharedInstance] deteleE2ETokenLockWithServerUrl:serverUrl];
  294. dispatch_semaphore_signal(semaphore);
  295. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  296. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_unlock_"];
  297. dispatch_semaphore_signal(semaphore);
  298. }];
  299. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  300. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_mark_folder_"];
  301. // UNLOCK
  302. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] ocId:ocId token:token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  303. [[NCManageDatabase sharedInstance] deteleE2ETokenLockWithServerUrl:serverUrl];
  304. dispatch_semaphore_signal(semaphore);
  305. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  306. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_unlock_"];
  307. dispatch_semaphore_signal(semaphore);
  308. }];
  309. }];
  310. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  311. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_lock_"];
  312. dispatch_semaphore_signal(semaphore);
  313. }];
  314. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer) {
  315. returnError = [self getError:response error:error descriptionDefault:@"_error_"];
  316. dispatch_semaphore_signal(semaphore);
  317. }];
  318. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  319. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  320. return returnError;
  321. }
  322. - (NSError *)deletemarkEndToEndFolderEncryptedOnServerUrl:(NSString *)serverUrl ocId:(NSString *)ocId user:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url
  323. {
  324. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  325. __block NSError *returnError = nil;
  326. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  327. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  328. [communication setUserAgent:[CCUtility getUserAgent]];
  329. tableE2eEncryptionLock *tableLock = [[NCManageDatabase sharedInstance] getE2ETokenLockWithServerUrl:serverUrl];
  330. // Read Folder
  331. [communication readFolder:serverUrl depth:@"1" withUserSessionToken:nil onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer, NSString *tokenReadFolder) {
  332. if (items.count > 1) {
  333. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:999 userInfo:[NSDictionary dictionaryWithObject:NSLocalizedString(@"_e2e_error_directory_not_empty_", nil) forKey:NSLocalizedDescriptionKey]];
  334. dispatch_semaphore_signal(semaphore);
  335. return;
  336. }
  337. // LOCK
  338. [communication lockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] ocId:ocId token:tableLock.token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer) {
  339. [[NCManageDatabase sharedInstance] setE2ETokenLockWithServerUrl:serverUrl ocId:ocId token:token];
  340. // DELETE METADATA
  341. [communication deleteEndToEndMetadata:[url stringByAppendingString:@"/"] ocId:ocId onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  342. NSLog(@"[LOG] Found metadata and delete");
  343. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  344. NSLog(@"[LOG] %@", [NSString stringWithFormat:@"Remove metadata error %d", (int)response.statusCode]);
  345. }];
  346. // DELETE MARK
  347. [communication deletemarkEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] ocId:ocId onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  348. // UNLOCK
  349. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] ocId:ocId token:token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  350. [[NCManageDatabase sharedInstance] deteleE2ETokenLockWithServerUrl:serverUrl];
  351. dispatch_semaphore_signal(semaphore);
  352. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  353. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_unlock_"];
  354. dispatch_semaphore_signal(semaphore);
  355. }];
  356. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  357. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_delete_mark_folder_"];
  358. // UNLOCK
  359. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] ocId:ocId token:token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  360. [[NCManageDatabase sharedInstance] deteleE2ETokenLockWithServerUrl:serverUrl];
  361. dispatch_semaphore_signal(semaphore);
  362. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  363. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_unlock_"];
  364. dispatch_semaphore_signal(semaphore);
  365. }];
  366. }];
  367. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  368. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_lock_"];
  369. dispatch_semaphore_signal(semaphore);
  370. }];
  371. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer) {
  372. returnError = [self getError:response error:error descriptionDefault:@"_error_"];
  373. dispatch_semaphore_signal(semaphore);
  374. }];
  375. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  376. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  377. return returnError;
  378. }
  379. - (NSError *)getEndToEndMetadata:(NSString **)metadata ocId:(NSString *)ocId user:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url
  380. {
  381. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  382. __block NSError *returnError = nil;
  383. __block NSString *returnMetadata = nil;
  384. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  385. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  386. [communication setUserAgent:[CCUtility getUserAgent]];
  387. [communication getEndToEndMetadata:[url stringByAppendingString:@"/"] ocId:ocId onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *encryptedMetadata, NSString *redirectedServer) {
  388. returnMetadata = encryptedMetadata;
  389. dispatch_semaphore_signal(semaphore);
  390. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  391. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_get_metadata_"];
  392. dispatch_semaphore_signal(semaphore);
  393. }];
  394. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  395. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  396. *metadata = returnMetadata;
  397. return returnError;
  398. }
  399. - (NSError *)deleteEndToEndMetadataOnServerUrl:(NSString *)serverUrl ocId:(NSString *)ocId unlock:(BOOL)unlock user:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url
  400. {
  401. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  402. __block NSError *returnError = nil;
  403. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  404. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  405. [communication setUserAgent:[CCUtility getUserAgent]];
  406. tableE2eEncryptionLock *tableLock = [[NCManageDatabase sharedInstance] getE2ETokenLockWithServerUrl:serverUrl];
  407. // LOCK
  408. [communication lockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] ocId:ocId token:tableLock.token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer) {
  409. [[NCManageDatabase sharedInstance] setE2ETokenLockWithServerUrl:serverUrl ocId:ocId token:token];
  410. // DELETE METADATA
  411. [communication deleteEndToEndMetadata:[url stringByAppendingString:@"/"] ocId:ocId onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  412. // UNLOCK
  413. if (unlock) {
  414. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] ocId:ocId token:token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  415. [[NCManageDatabase sharedInstance] deteleE2ETokenLockWithServerUrl:serverUrl];
  416. dispatch_semaphore_signal(semaphore);
  417. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  418. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_unlock_"];
  419. dispatch_semaphore_signal(semaphore);
  420. }];
  421. } else {
  422. dispatch_semaphore_signal(semaphore);
  423. }
  424. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  425. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_delete_metadata_"];
  426. // UNLOCK
  427. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] ocId:ocId token:token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  428. [[NCManageDatabase sharedInstance] deteleE2ETokenLockWithServerUrl:serverUrl];
  429. dispatch_semaphore_signal(semaphore);
  430. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  431. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_unlock_"];
  432. dispatch_semaphore_signal(semaphore);
  433. }];
  434. }];
  435. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  436. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_lock_"];
  437. dispatch_semaphore_signal(semaphore);
  438. }];
  439. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  440. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  441. return returnError;
  442. }
  443. - (NSError *)storeEndToEndMetadata:(NSString *)metadata serverUrl:(NSString *)serverUrl ocId:(NSString *)ocId unlock:(BOOL)unlock user:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url
  444. {
  445. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  446. __block NSError *returnError = nil;
  447. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  448. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  449. [communication setUserAgent:[CCUtility getUserAgent]];
  450. tableE2eEncryptionLock *tableLock = [[NCManageDatabase sharedInstance] getE2ETokenLockWithServerUrl:serverUrl];
  451. // LOCK
  452. [communication lockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] ocId:ocId token:tableLock.token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer) {
  453. [[NCManageDatabase sharedInstance] setE2ETokenLockWithServerUrl:serverUrl ocId:ocId token:token];
  454. // STORE METADATA
  455. [communication storeEndToEndMetadata:[url stringByAppendingString:@"/"] ocId:ocId encryptedMetadata:metadata onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *encryptedMetadata, NSString *redirectedServer) {
  456. // UNLOCK
  457. if (unlock) {
  458. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] ocId:ocId token:token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  459. [[NCManageDatabase sharedInstance] deteleE2ETokenLockWithServerUrl:serverUrl];
  460. dispatch_semaphore_signal(semaphore);
  461. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  462. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_unlock_"];
  463. dispatch_semaphore_signal(semaphore);
  464. }];
  465. } else {
  466. dispatch_semaphore_signal(semaphore);
  467. }
  468. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  469. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_store_metadata_"];
  470. // UNLOCK
  471. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] ocId:ocId token:token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  472. [[NCManageDatabase sharedInstance] deteleE2ETokenLockWithServerUrl:serverUrl];
  473. dispatch_semaphore_signal(semaphore);
  474. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  475. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_unlock_"];
  476. dispatch_semaphore_signal(semaphore);
  477. }];
  478. }];
  479. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  480. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_lock_"];
  481. dispatch_semaphore_signal(semaphore);
  482. }];
  483. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  484. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  485. return returnError;
  486. }
  487. - (NSError *)updateEndToEndMetadata:(NSString *)metadata serverUrl:(NSString *)serverUrl ocId:(NSString *)ocId unlock:(BOOL)unlock user:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url
  488. {
  489. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  490. __block NSError *returnError = nil;
  491. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  492. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  493. [communication setUserAgent:[CCUtility getUserAgent]];
  494. tableE2eEncryptionLock *tableLock = [[NCManageDatabase sharedInstance] getE2ETokenLockWithServerUrl:serverUrl];
  495. // LOCK
  496. [communication lockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] ocId:ocId token:tableLock.token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer) {
  497. [[NCManageDatabase sharedInstance] setE2ETokenLockWithServerUrl:serverUrl ocId:ocId token:token];
  498. // UPDATA METADATA
  499. [communication updateEndToEndMetadata:[url stringByAppendingString:@"/"] ocId:ocId encryptedMetadata:metadata token:token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *encryptedMetadata, NSString *redirectedServer) {
  500. // UNLOCK
  501. if (unlock) {
  502. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] ocId:ocId token:token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  503. [[NCManageDatabase sharedInstance] deteleE2ETokenLockWithServerUrl:serverUrl];
  504. dispatch_semaphore_signal(semaphore);
  505. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  506. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_unlock_"];
  507. dispatch_semaphore_signal(semaphore);
  508. }];
  509. } else {
  510. dispatch_semaphore_signal(semaphore);
  511. }
  512. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  513. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_update_metadata_"];
  514. // UNLOCK
  515. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] ocId:ocId token:token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  516. [[NCManageDatabase sharedInstance] deteleE2ETokenLockWithServerUrl:serverUrl];
  517. dispatch_semaphore_signal(semaphore);
  518. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  519. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_unlock_"];
  520. dispatch_semaphore_signal(semaphore);
  521. }];
  522. }];
  523. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  524. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_lock_"];
  525. dispatch_semaphore_signal(semaphore);
  526. }];
  527. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  528. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  529. return returnError;
  530. }
  531. - (NSError *)lockEndToEndFolderEncryptedOnServerUrl:(NSString *)serverUrl ocId:(NSString *)ocId user:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url
  532. {
  533. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  534. __block NSError *returnError = nil;
  535. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  536. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  537. [communication setUserAgent:[CCUtility getUserAgent]];
  538. tableE2eEncryptionLock *tableLock = [[NCManageDatabase sharedInstance] getE2ETokenLockWithServerUrl:serverUrl];
  539. // LOCK
  540. [communication lockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] ocId:ocId token:tableLock.token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer) {
  541. [[NCManageDatabase sharedInstance] setE2ETokenLockWithServerUrl:serverUrl ocId:ocId token:token];
  542. dispatch_semaphore_signal(semaphore);
  543. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  544. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_lock_"];
  545. dispatch_semaphore_signal(semaphore);
  546. }];
  547. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  548. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  549. return returnError;
  550. }
  551. - (NSError *)unlockEndToEndFolderEncryptedOnServerUrl:(NSString *)serverUrl ocId:(NSString *)ocId token:(NSString *)token user:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url
  552. {
  553. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  554. __block NSError *returnError = nil;
  555. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  556. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  557. [communication setUserAgent:[CCUtility getUserAgent]];
  558. // UNLOCK
  559. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] ocId:ocId token:token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  560. [[NCManageDatabase sharedInstance] deteleE2ETokenLockWithServerUrl:serverUrl];
  561. dispatch_semaphore_signal(semaphore);
  562. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  563. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_unlock_"];
  564. dispatch_semaphore_signal(semaphore);
  565. }];
  566. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  567. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  568. return returnError;
  569. }
  570. - (NSError *)sendEndToEndMetadataOnServerUrl:(NSString *)serverUrl fileNameRename:(NSString *)fileName fileNameNewRename:(NSString *)fileNameNew account:(NSString *)account user:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url
  571. {
  572. tableDirectory *directory = [[NCManageDatabase sharedInstance] getTableDirectoryWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@", account, serverUrl]];
  573. NSString *metadata;
  574. NSError *error;
  575. // Enabled E2E
  576. if ([CCUtility isEndToEndEnabled:account] == NO)
  577. return [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:k_CCErrorInternalError userInfo:[NSDictionary dictionaryWithObject:NSLocalizedString(@"_e2e_error_not_enabled_", nil) forKey:NSLocalizedDescriptionKey]];
  578. // get Metadata for select updateEndToEndMetadata or storeEndToEndMetadata
  579. error = [self getEndToEndMetadata:&metadata ocId:directory.ocId user:user userID:userID password:password url:url];
  580. if (error.code != kOCErrorServerPathNotFound && error != nil) {
  581. return error;
  582. }
  583. // Rename
  584. if (fileName && fileNameNew)
  585. [[NCManageDatabase sharedInstance] renameFileE2eEncryptionWithServerUrl:serverUrl fileNameIdentifier:fileName newFileName:fileNameNew newFileNamePath:[CCUtility returnFileNamePathFromFileName:fileNameNew serverUrl:serverUrl activeUrl:url]];
  586. NSArray *tableE2eEncryption = [[NCManageDatabase sharedInstance] getE2eEncryptionsWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@", account, serverUrl]];
  587. if (!tableE2eEncryption)
  588. return [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:k_CCErrorInternalError userInfo:[NSDictionary dictionaryWithObject:NSLocalizedString(@"_e2e_error_record_not_found_", nil) forKey:NSLocalizedDescriptionKey]];
  589. NSString *e2eMetadataJSON = [[NCEndToEndMetadata sharedInstance] encoderMetadata:tableE2eEncryption privateKey:[CCUtility getEndToEndPrivateKey:account] serverUrl:serverUrl];
  590. if (!e2eMetadataJSON)
  591. return [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:k_CCErrorInternalError userInfo:[NSDictionary dictionaryWithObject:NSLocalizedString(@"_e2e_error_encode_metadata_", nil) forKey:NSLocalizedDescriptionKey]];
  592. // send Metadata
  593. if (error == nil)
  594. error = [self updateEndToEndMetadata:e2eMetadataJSON serverUrl:serverUrl ocId:directory.ocId unlock:NO user:user userID:userID password:password url:url];
  595. else if (error.code == kOCErrorServerPathNotFound)
  596. error = [self storeEndToEndMetadata:e2eMetadataJSON serverUrl:serverUrl ocId:directory.ocId unlock:NO user:user userID:userID password:password url:url];
  597. return error;
  598. }
  599. - (NSError *)rebuildAndSendEndToEndMetadataOnServerUrl:(NSString *)serverUrl account:(NSString *)account user:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url
  600. {
  601. NSError *error;
  602. NSString *e2eMetadataJSON;
  603. tableDirectory *directory = [[NCManageDatabase sharedInstance] getTableDirectoryWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@", account, serverUrl]];
  604. if (directory.e2eEncrypted == NO)
  605. return nil;
  606. NSArray *tableE2eEncryption = [[NCManageDatabase sharedInstance] getE2eEncryptionsWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@", account, serverUrl]];
  607. if (tableE2eEncryption) {
  608. e2eMetadataJSON = [[NCEndToEndMetadata sharedInstance] encoderMetadata:tableE2eEncryption privateKey:[CCUtility getEndToEndPrivateKey:account] serverUrl:serverUrl];
  609. if (!e2eMetadataJSON)
  610. return [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:k_CCErrorInternalError userInfo:[NSDictionary dictionaryWithObject:NSLocalizedString(@"_e2e_error_encode_metadata_", nil) forKey:NSLocalizedDescriptionKey]];
  611. error = [self updateEndToEndMetadata:e2eMetadataJSON serverUrl:serverUrl ocId:directory.ocId unlock:YES user:user userID:userID password:password url:url];
  612. } else {
  613. [self deleteEndToEndMetadataOnServerUrl:serverUrl ocId:directory.ocId unlock:YES user:user userID:userID password:password url:url];
  614. }
  615. return error;
  616. }
  617. - (NSError *)getError:(NSHTTPURLResponse *)response error:(NSError *)error descriptionDefault:(NSString *)descriptionDefault
  618. {
  619. NSInteger errorCode = response.statusCode;
  620. NSString *errorDescription = response.description;
  621. if (errorDescription == nil || errorCode == 0) {
  622. errorCode = error.code;
  623. errorDescription = error.description;
  624. if (errorDescription == nil) errorDescription = NSLocalizedString(descriptionDefault, @"");
  625. }
  626. errorDescription = [NSString stringWithFormat:@"%@ [%ld] - %@", NSLocalizedString(descriptionDefault, @""), (long)errorCode, errorDescription];
  627. if (errorDescription.length >= 250) {
  628. errorDescription = [errorDescription substringToIndex:250];
  629. errorDescription = [errorDescription stringByAppendingString:@" ..."];
  630. }
  631. return [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:errorCode userInfo:[NSDictionary dictionaryWithObject:errorDescription forKey:NSLocalizedDescriptionKey]];
  632. }
  633. @end