NCNetworkingEndToEnd.m 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  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 fileId:(NSString **)fileId error:(NSError **)error
  222. {
  223. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  224. __block NSError *returnError = nil;
  225. __block NSString *returnFileId = nil;
  226. __block NSString *returnOcId = nil;
  227. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  228. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  229. [communication setUserAgent:[CCUtility getUserAgent]];
  230. [communication readFile:folderPathName onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer) {
  231. dispatch_semaphore_signal(semaphore);
  232. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  233. [communication createFolder:folderPathName onCommunication:communication withForbiddenCharactersSupported:YES successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  234. NSDictionary *fields = [response allHeaderFields];
  235. returnOcId = [CCUtility removeForbiddenCharactersFileSystem:[fields objectForKey:@"OC-FileId"]];
  236. returnFileId = [CCUtility convertOcIdToFileId:returnOcId];
  237. if (encrypted) {
  238. // MARK
  239. [communication markEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileId:returnFileId onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  240. [[NCManageDatabase sharedInstance] clearDateReadWithServerUrl:[CCUtility deletingLastPathComponentFromServerUrl:folderPathName] account:account];
  241. dispatch_semaphore_signal(semaphore);
  242. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  243. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_mark_folder_"];
  244. dispatch_semaphore_signal(semaphore);
  245. }];
  246. } else {
  247. [[NCManageDatabase sharedInstance] clearDateReadWithServerUrl:[CCUtility deletingLastPathComponentFromServerUrl:folderPathName] account:account];
  248. dispatch_semaphore_signal(semaphore);
  249. }
  250. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  251. returnError = [self getError:response error:error descriptionDefault:@"_error_"];
  252. dispatch_semaphore_signal(semaphore);
  253. } errorBeforeRequest:^(NSError *error) {
  254. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:response.description forKey:NSLocalizedDescriptionKey]];
  255. dispatch_semaphore_signal(semaphore);
  256. }];
  257. }];
  258. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  259. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  260. *ocId = returnOcId;
  261. *fileId = returnFileId;
  262. *error = returnError;
  263. }
  264. #pragma --------------------------------------------------------------------------------------------
  265. #pragma mark ===== E2EE End-to-End Encryption =====
  266. #pragma --------------------------------------------------------------------------------------------
  267. // E2EE
  268. - (NSError *)markEndToEndFolderEncryptedOnServerUrl:(NSString *)serverUrl fileId:(NSString *)fileId user:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url
  269. {
  270. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  271. __block NSError *returnError = nil;
  272. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  273. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  274. [communication setUserAgent:[CCUtility getUserAgent]];
  275. tableE2eEncryptionLock *tableLock = [[NCManageDatabase sharedInstance] getE2ETokenLockWithServerUrl:serverUrl];
  276. // Read Folder
  277. [communication readFolder:serverUrl depth:@"1" withUserSessionToken:nil onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer, NSString *tokenReadFolder) {
  278. if (items.count > 1) {
  279. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:999 userInfo:[NSDictionary dictionaryWithObject:NSLocalizedString(@"_e2e_error_directory_not_empty_", nil) forKey:NSLocalizedDescriptionKey]];
  280. dispatch_semaphore_signal(semaphore);
  281. return;
  282. }
  283. // LOCK
  284. [communication lockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileId:fileId token:tableLock.token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer) {
  285. [[NCManageDatabase sharedInstance] setE2ETokenLockWithServerUrl:serverUrl fileId:fileId token:token];
  286. // REMOVE METADATA
  287. [communication deleteEndToEndMetadata:[url stringByAppendingString:@"/"] fileId:fileId onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  288. NSLog(@"[LOG] Found metadata and delete");
  289. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  290. NSLog(@"[LOG] %@", [NSString stringWithFormat:@"Remove metadata error %d", (int)response.statusCode]);
  291. }];
  292. // MARK
  293. [communication markEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileId:fileId onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  294. // UNLOCK
  295. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileId:fileId token:token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  296. [[NCManageDatabase sharedInstance] deteleE2ETokenLockWithServerUrl:serverUrl];
  297. dispatch_semaphore_signal(semaphore);
  298. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  299. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_unlock_"];
  300. dispatch_semaphore_signal(semaphore);
  301. }];
  302. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  303. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_mark_folder_"];
  304. // UNLOCK
  305. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileId:fileId token:token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  306. [[NCManageDatabase sharedInstance] deteleE2ETokenLockWithServerUrl:serverUrl];
  307. dispatch_semaphore_signal(semaphore);
  308. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  309. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_unlock_"];
  310. dispatch_semaphore_signal(semaphore);
  311. }];
  312. }];
  313. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  314. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_lock_"];
  315. dispatch_semaphore_signal(semaphore);
  316. }];
  317. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer) {
  318. returnError = [self getError:response error:error descriptionDefault:@"_error_"];
  319. dispatch_semaphore_signal(semaphore);
  320. }];
  321. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  322. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  323. return returnError;
  324. }
  325. - (NSError *)deletemarkEndToEndFolderEncryptedOnServerUrl:(NSString *)serverUrl fileId:(NSString *)fileId user:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url
  326. {
  327. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  328. __block NSError *returnError = nil;
  329. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  330. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  331. [communication setUserAgent:[CCUtility getUserAgent]];
  332. tableE2eEncryptionLock *tableLock = [[NCManageDatabase sharedInstance] getE2ETokenLockWithServerUrl:serverUrl];
  333. // Read Folder
  334. [communication readFolder:serverUrl depth:@"1" withUserSessionToken:nil onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer, NSString *tokenReadFolder) {
  335. if (items.count > 1) {
  336. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:999 userInfo:[NSDictionary dictionaryWithObject:NSLocalizedString(@"_e2e_error_directory_not_empty_", nil) forKey:NSLocalizedDescriptionKey]];
  337. dispatch_semaphore_signal(semaphore);
  338. return;
  339. }
  340. // LOCK
  341. [communication lockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileId:fileId token:tableLock.token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer) {
  342. [[NCManageDatabase sharedInstance] setE2ETokenLockWithServerUrl:serverUrl fileId:fileId token:token];
  343. // DELETE METADATA
  344. [communication deleteEndToEndMetadata:[url stringByAppendingString:@"/"] fileId:fileId onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  345. NSLog(@"[LOG] Found metadata and delete");
  346. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  347. NSLog(@"[LOG] %@", [NSString stringWithFormat:@"Remove metadata error %d", (int)response.statusCode]);
  348. }];
  349. // DELETE MARK
  350. [communication deletemarkEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileId:fileId onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  351. // UNLOCK
  352. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileId:fileId token:token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  353. [[NCManageDatabase sharedInstance] deteleE2ETokenLockWithServerUrl:serverUrl];
  354. dispatch_semaphore_signal(semaphore);
  355. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  356. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_unlock_"];
  357. dispatch_semaphore_signal(semaphore);
  358. }];
  359. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  360. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_delete_mark_folder_"];
  361. // UNLOCK
  362. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileId:fileId token:token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  363. [[NCManageDatabase sharedInstance] deteleE2ETokenLockWithServerUrl:serverUrl];
  364. dispatch_semaphore_signal(semaphore);
  365. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  366. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_unlock_"];
  367. dispatch_semaphore_signal(semaphore);
  368. }];
  369. }];
  370. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  371. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_lock_"];
  372. dispatch_semaphore_signal(semaphore);
  373. }];
  374. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer) {
  375. returnError = [self getError:response error:error descriptionDefault:@"_error_"];
  376. dispatch_semaphore_signal(semaphore);
  377. }];
  378. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  379. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  380. return returnError;
  381. }
  382. - (NSError *)getEndToEndMetadata:(NSString **)metadata fileId:(NSString *)fileId user:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url
  383. {
  384. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  385. __block NSError *returnError = nil;
  386. __block NSString *returnMetadata = nil;
  387. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  388. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  389. [communication setUserAgent:[CCUtility getUserAgent]];
  390. [communication getEndToEndMetadata:[url stringByAppendingString:@"/"] fileId:fileId onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *encryptedMetadata, NSString *redirectedServer) {
  391. returnMetadata = encryptedMetadata;
  392. dispatch_semaphore_signal(semaphore);
  393. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  394. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_get_metadata_"];
  395. dispatch_semaphore_signal(semaphore);
  396. }];
  397. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  398. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  399. *metadata = returnMetadata;
  400. return returnError;
  401. }
  402. - (NSError *)deleteEndToEndMetadataOnServerUrl:(NSString *)serverUrl fileId:(NSString *)fileId unlock:(BOOL)unlock user:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url
  403. {
  404. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  405. __block NSError *returnError = nil;
  406. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  407. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  408. [communication setUserAgent:[CCUtility getUserAgent]];
  409. tableE2eEncryptionLock *tableLock = [[NCManageDatabase sharedInstance] getE2ETokenLockWithServerUrl:serverUrl];
  410. // LOCK
  411. [communication lockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileId:fileId token:tableLock.token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer) {
  412. [[NCManageDatabase sharedInstance] setE2ETokenLockWithServerUrl:serverUrl fileId:fileId token:token];
  413. // DELETE METADATA
  414. [communication deleteEndToEndMetadata:[url stringByAppendingString:@"/"] fileId:fileId onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  415. // UNLOCK
  416. if (unlock) {
  417. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileId:fileId token:token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  418. [[NCManageDatabase sharedInstance] deteleE2ETokenLockWithServerUrl:serverUrl];
  419. dispatch_semaphore_signal(semaphore);
  420. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  421. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_unlock_"];
  422. dispatch_semaphore_signal(semaphore);
  423. }];
  424. } else {
  425. dispatch_semaphore_signal(semaphore);
  426. }
  427. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  428. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_delete_metadata_"];
  429. // UNLOCK
  430. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileId:fileId token:token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  431. [[NCManageDatabase sharedInstance] deteleE2ETokenLockWithServerUrl:serverUrl];
  432. dispatch_semaphore_signal(semaphore);
  433. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  434. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_unlock_"];
  435. dispatch_semaphore_signal(semaphore);
  436. }];
  437. }];
  438. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  439. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_lock_"];
  440. dispatch_semaphore_signal(semaphore);
  441. }];
  442. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  443. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  444. return returnError;
  445. }
  446. - (NSError *)storeEndToEndMetadata:(NSString *)metadata serverUrl:(NSString *)serverUrl fileId:(NSString *)fileId unlock:(BOOL)unlock user:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url
  447. {
  448. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  449. __block NSError *returnError = nil;
  450. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  451. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  452. [communication setUserAgent:[CCUtility getUserAgent]];
  453. tableE2eEncryptionLock *tableLock = [[NCManageDatabase sharedInstance] getE2ETokenLockWithServerUrl:serverUrl];
  454. // LOCK
  455. [communication lockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileId:fileId token:tableLock.token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer) {
  456. [[NCManageDatabase sharedInstance] setE2ETokenLockWithServerUrl:serverUrl fileId:fileId token:token];
  457. // STORE METADATA
  458. [communication storeEndToEndMetadata:[url stringByAppendingString:@"/"] fileId:fileId encryptedMetadata:metadata onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *encryptedMetadata, NSString *redirectedServer) {
  459. // UNLOCK
  460. if (unlock) {
  461. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileId:fileId token:token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  462. [[NCManageDatabase sharedInstance] deteleE2ETokenLockWithServerUrl:serverUrl];
  463. dispatch_semaphore_signal(semaphore);
  464. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  465. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_unlock_"];
  466. dispatch_semaphore_signal(semaphore);
  467. }];
  468. } else {
  469. dispatch_semaphore_signal(semaphore);
  470. }
  471. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  472. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_store_metadata_"];
  473. // UNLOCK
  474. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileId:fileId token:token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  475. [[NCManageDatabase sharedInstance] deteleE2ETokenLockWithServerUrl:serverUrl];
  476. dispatch_semaphore_signal(semaphore);
  477. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  478. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_unlock_"];
  479. dispatch_semaphore_signal(semaphore);
  480. }];
  481. }];
  482. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  483. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_lock_"];
  484. dispatch_semaphore_signal(semaphore);
  485. }];
  486. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  487. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  488. return returnError;
  489. }
  490. - (NSError *)updateEndToEndMetadata:(NSString *)metadata serverUrl:(NSString *)serverUrl fileId:(NSString *)fileId unlock:(BOOL)unlock user:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url
  491. {
  492. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  493. __block NSError *returnError = nil;
  494. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  495. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  496. [communication setUserAgent:[CCUtility getUserAgent]];
  497. tableE2eEncryptionLock *tableLock = [[NCManageDatabase sharedInstance] getE2ETokenLockWithServerUrl:serverUrl];
  498. // LOCK
  499. [communication lockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileId:fileId token:tableLock.token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer) {
  500. [[NCManageDatabase sharedInstance] setE2ETokenLockWithServerUrl:serverUrl fileId:fileId token:token];
  501. // UPDATA METADATA
  502. [communication updateEndToEndMetadata:[url stringByAppendingString:@"/"] fileId:fileId encryptedMetadata:metadata token:token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *encryptedMetadata, NSString *redirectedServer) {
  503. // UNLOCK
  504. if (unlock) {
  505. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileId:fileId token:token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  506. [[NCManageDatabase sharedInstance] deteleE2ETokenLockWithServerUrl:serverUrl];
  507. dispatch_semaphore_signal(semaphore);
  508. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  509. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_unlock_"];
  510. dispatch_semaphore_signal(semaphore);
  511. }];
  512. } else {
  513. dispatch_semaphore_signal(semaphore);
  514. }
  515. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  516. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_update_metadata_"];
  517. // UNLOCK
  518. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileId:fileId token:token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  519. [[NCManageDatabase sharedInstance] deteleE2ETokenLockWithServerUrl:serverUrl];
  520. dispatch_semaphore_signal(semaphore);
  521. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  522. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_unlock_"];
  523. dispatch_semaphore_signal(semaphore);
  524. }];
  525. }];
  526. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  527. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_lock_"];
  528. dispatch_semaphore_signal(semaphore);
  529. }];
  530. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  531. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  532. return returnError;
  533. }
  534. - (NSError *)lockEndToEndFolderEncryptedOnServerUrl:(NSString *)serverUrl fileId:(NSString *)fileId user:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url
  535. {
  536. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  537. __block NSError *returnError = nil;
  538. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  539. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  540. [communication setUserAgent:[CCUtility getUserAgent]];
  541. tableE2eEncryptionLock *tableLock = [[NCManageDatabase sharedInstance] getE2ETokenLockWithServerUrl:serverUrl];
  542. // LOCK
  543. [communication lockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileId:fileId token:tableLock.token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer) {
  544. [[NCManageDatabase sharedInstance] setE2ETokenLockWithServerUrl:serverUrl fileId:fileId token:token];
  545. dispatch_semaphore_signal(semaphore);
  546. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  547. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_lock_"];
  548. dispatch_semaphore_signal(semaphore);
  549. }];
  550. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  551. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  552. return returnError;
  553. }
  554. - (NSError *)unlockEndToEndFolderEncryptedOnServerUrl:(NSString *)serverUrl fileId:(NSString *)fileId token:(NSString *)token user:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url
  555. {
  556. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  557. __block NSError *returnError = nil;
  558. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  559. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  560. [communication setUserAgent:[CCUtility getUserAgent]];
  561. // UNLOCK
  562. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileId:fileId token:token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  563. [[NCManageDatabase sharedInstance] deteleE2ETokenLockWithServerUrl:serverUrl];
  564. dispatch_semaphore_signal(semaphore);
  565. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  566. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_unlock_"];
  567. dispatch_semaphore_signal(semaphore);
  568. }];
  569. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  570. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  571. return returnError;
  572. }
  573. - (NSError *)sendEndToEndMetadataOnServerUrl:(NSString *)serverUrl fileNameRename:(NSString *)fileName fileNameNewRename:(NSString *)fileNameNew unlock:(BOOL)unlock account:(NSString *)account user:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url
  574. {
  575. tableDirectory *directory = [[NCManageDatabase sharedInstance] getTableDirectoryWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@", account, serverUrl]];
  576. NSString *metadata;
  577. NSError *error;
  578. // Enabled E2E
  579. if ([CCUtility isEndToEndEnabled:account] == NO)
  580. return [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:k_CCErrorInternalError userInfo:[NSDictionary dictionaryWithObject:NSLocalizedString(@"_e2e_error_not_enabled_", nil) forKey:NSLocalizedDescriptionKey]];
  581. // get Metadata for select updateEndToEndMetadata or storeEndToEndMetadata
  582. error = [self getEndToEndMetadata:&metadata fileId:directory.fileId user:user userID:userID password:password url:url];
  583. if (error.code != kOCErrorServerPathNotFound && error != nil) {
  584. return error;
  585. }
  586. // Rename
  587. if (fileName && fileNameNew)
  588. [[NCManageDatabase sharedInstance] renameFileE2eEncryptionWithServerUrl:serverUrl fileNameIdentifier:fileName newFileName:fileNameNew newFileNamePath:[CCUtility returnFileNamePathFromFileName:fileNameNew serverUrl:serverUrl activeUrl:url]];
  589. NSArray *tableE2eEncryption = [[NCManageDatabase sharedInstance] getE2eEncryptionsWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@", account, serverUrl]];
  590. if (!tableE2eEncryption)
  591. return [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:k_CCErrorInternalError userInfo:[NSDictionary dictionaryWithObject:NSLocalizedString(@"_e2e_error_record_not_found_", nil) forKey:NSLocalizedDescriptionKey]];
  592. NSString *e2eMetadataJSON = [[NCEndToEndMetadata sharedInstance] encoderMetadata:tableE2eEncryption privateKey:[CCUtility getEndToEndPrivateKey:account] serverUrl:serverUrl];
  593. if (!e2eMetadataJSON)
  594. return [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:k_CCErrorInternalError userInfo:[NSDictionary dictionaryWithObject:NSLocalizedString(@"_e2e_error_encode_metadata_", nil) forKey:NSLocalizedDescriptionKey]];
  595. // send Metadata
  596. if (error == nil)
  597. error = [self updateEndToEndMetadata:e2eMetadataJSON serverUrl:serverUrl fileId:directory.fileId unlock:unlock user:user userID:userID password:password url:url];
  598. else if (error.code == kOCErrorServerPathNotFound)
  599. error = [self storeEndToEndMetadata:e2eMetadataJSON serverUrl:serverUrl fileId:directory.fileId unlock:unlock user:user userID:userID password:password url:url];
  600. return error;
  601. }
  602. - (NSError *)rebuildAndSendEndToEndMetadataOnServerUrl:(NSString *)serverUrl account:(NSString *)account user:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url
  603. {
  604. NSError *error;
  605. NSString *e2eMetadataJSON;
  606. tableDirectory *directory = [[NCManageDatabase sharedInstance] getTableDirectoryWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@", account, serverUrl]];
  607. if (directory.e2eEncrypted == NO)
  608. return nil;
  609. NSArray *tableE2eEncryption = [[NCManageDatabase sharedInstance] getE2eEncryptionsWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@", account, serverUrl]];
  610. if (tableE2eEncryption) {
  611. e2eMetadataJSON = [[NCEndToEndMetadata sharedInstance] encoderMetadata:tableE2eEncryption privateKey:[CCUtility getEndToEndPrivateKey:account] serverUrl:serverUrl];
  612. if (!e2eMetadataJSON)
  613. return [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:k_CCErrorInternalError userInfo:[NSDictionary dictionaryWithObject:NSLocalizedString(@"_e2e_error_encode_metadata_", nil) forKey:NSLocalizedDescriptionKey]];
  614. error = [self updateEndToEndMetadata:e2eMetadataJSON serverUrl:serverUrl fileId:directory.fileId unlock:YES user:user userID:userID password:password url:url];
  615. } else {
  616. [self deleteEndToEndMetadataOnServerUrl:serverUrl fileId:directory.fileId unlock:YES user:user userID:userID password:password url:url];
  617. }
  618. return error;
  619. }
  620. - (NSError *)getError:(NSHTTPURLResponse *)response error:(NSError *)error descriptionDefault:(NSString *)descriptionDefault
  621. {
  622. NSInteger errorCode = response.statusCode;
  623. NSString *errorDescription = response.description;
  624. if (errorDescription == nil || errorCode == 0) {
  625. errorCode = error.code;
  626. errorDescription = error.description;
  627. if (errorDescription == nil) errorDescription = NSLocalizedString(descriptionDefault, @"");
  628. }
  629. errorDescription = [NSString stringWithFormat:@"%@ [%ld] - %@", NSLocalizedString(descriptionDefault, @""), (long)errorCode, errorDescription];
  630. if (errorDescription.length >= 250) {
  631. errorDescription = [errorDescription substringToIndex:250];
  632. errorDescription = [errorDescription stringByAppendingString:@" ..."];
  633. }
  634. return [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:errorCode userInfo:[NSDictionary dictionaryWithObject:errorDescription forKey:NSLocalizedDescriptionKey]];
  635. }
  636. @end