NCNetworkingEndToEnd.m 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  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. // MARK
  276. [communication markEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileId:fileId onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  277. dispatch_semaphore_signal(semaphore);
  278. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  279. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_mark_folder_"];
  280. dispatch_semaphore_signal(semaphore);
  281. }];
  282. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  283. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  284. return returnError;
  285. }
  286. - (NSError *)deletemarkEndToEndFolderEncryptedOnServerUrl:(NSString *)serverUrl fileId:(NSString *)fileId user:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url
  287. {
  288. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  289. __block NSError *returnError = nil;
  290. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  291. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  292. [communication setUserAgent:[CCUtility getUserAgent]];
  293. tableE2eEncryptionLock *tableLock = [[NCManageDatabase sharedInstance] getE2ETokenLockWithServerUrl:serverUrl];
  294. // LOCK
  295. [communication lockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileId:fileId e2eToken:tableLock.e2eToken onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *e2eToken, NSString *redirectedServer) {
  296. [[NCManageDatabase sharedInstance] setE2ETokenLockWithServerUrl:serverUrl fileId:fileId e2eToken:e2eToken];
  297. // DELETE MARK
  298. [communication deletemarkEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileId:fileId e2eToken:e2eToken onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  299. // UNLOCK
  300. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileId:fileId e2eToken:e2eToken onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  301. [[NCManageDatabase sharedInstance] deteleE2ETokenLockWithServerUrl:serverUrl];
  302. dispatch_semaphore_signal(semaphore);
  303. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  304. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_unlock_"];
  305. dispatch_semaphore_signal(semaphore);
  306. }];
  307. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  308. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_delete_mark_folder_"];
  309. // UNLOCK
  310. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileId:fileId e2eToken:e2eToken onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  311. [[NCManageDatabase sharedInstance] deteleE2ETokenLockWithServerUrl:serverUrl];
  312. dispatch_semaphore_signal(semaphore);
  313. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  314. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_unlock_"];
  315. dispatch_semaphore_signal(semaphore);
  316. }];
  317. }];
  318. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  319. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_lock_"];
  320. dispatch_semaphore_signal(semaphore);
  321. }];
  322. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  323. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  324. return returnError;
  325. }
  326. - (NSError *)getEndToEndMetadata:(NSString **)metadata fileId:(NSString *)fileId user:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url
  327. {
  328. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  329. __block NSError *returnError = nil;
  330. __block NSString *returnMetadata = nil;
  331. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  332. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  333. [communication setUserAgent:[CCUtility getUserAgent]];
  334. [communication getEndToEndMetadata:[url stringByAppendingString:@"/"] fileId:fileId onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *encryptedMetadata, NSString *redirectedServer) {
  335. returnMetadata = encryptedMetadata;
  336. dispatch_semaphore_signal(semaphore);
  337. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  338. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_get_metadata_"];
  339. dispatch_semaphore_signal(semaphore);
  340. }];
  341. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  342. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  343. *metadata = returnMetadata;
  344. return returnError;
  345. }
  346. - (NSError *)deleteEndToEndMetadataOnServerUrl:(NSString *)serverUrl fileId:(NSString *)fileId unlock:(BOOL)unlock user:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url
  347. {
  348. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  349. __block NSError *returnError = nil;
  350. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  351. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  352. [communication setUserAgent:[CCUtility getUserAgent]];
  353. tableE2eEncryptionLock *tableLock = [[NCManageDatabase sharedInstance] getE2ETokenLockWithServerUrl:serverUrl];
  354. // LOCK
  355. [communication lockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileId:fileId e2eToken:tableLock.e2eToken onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *e2eToken, NSString *redirectedServer) {
  356. [[NCManageDatabase sharedInstance] setE2ETokenLockWithServerUrl:serverUrl fileId:fileId e2eToken:e2eToken];
  357. // DELETE METADATA
  358. [communication deleteEndToEndMetadata:[url stringByAppendingString:@"/"] fileId:fileId e2eToken:e2eToken onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  359. // UNLOCK
  360. if (unlock) {
  361. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileId:fileId e2eToken:e2eToken onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  362. [[NCManageDatabase sharedInstance] deteleE2ETokenLockWithServerUrl:serverUrl];
  363. dispatch_semaphore_signal(semaphore);
  364. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  365. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_unlock_"];
  366. dispatch_semaphore_signal(semaphore);
  367. }];
  368. } else {
  369. dispatch_semaphore_signal(semaphore);
  370. }
  371. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  372. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_delete_metadata_"];
  373. // UNLOCK
  374. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileId:fileId e2eToken:e2eToken onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  375. [[NCManageDatabase sharedInstance] deteleE2ETokenLockWithServerUrl:serverUrl];
  376. dispatch_semaphore_signal(semaphore);
  377. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  378. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_unlock_"];
  379. dispatch_semaphore_signal(semaphore);
  380. }];
  381. }];
  382. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  383. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_lock_"];
  384. dispatch_semaphore_signal(semaphore);
  385. }];
  386. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  387. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  388. return returnError;
  389. }
  390. - (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
  391. {
  392. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  393. __block NSError *returnError = nil;
  394. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  395. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  396. [communication setUserAgent:[CCUtility getUserAgent]];
  397. tableE2eEncryptionLock *tableLock = [[NCManageDatabase sharedInstance] getE2ETokenLockWithServerUrl:serverUrl];
  398. // LOCK
  399. [communication lockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileId:fileId e2eToken:tableLock.e2eToken onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *e2eToken, NSString *redirectedServer) {
  400. [[NCManageDatabase sharedInstance] setE2ETokenLockWithServerUrl:serverUrl fileId:fileId e2eToken:e2eToken];
  401. // STORE METADATA
  402. [communication storeEndToEndMetadata:[url stringByAppendingString:@"/"] fileId:fileId e2eToken:e2eToken encryptedMetadata:metadata onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *encryptedMetadata, NSString *redirectedServer) {
  403. // UNLOCK
  404. if (unlock) {
  405. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileId:fileId e2eToken:e2eToken onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  406. [[NCManageDatabase sharedInstance] deteleE2ETokenLockWithServerUrl:serverUrl];
  407. dispatch_semaphore_signal(semaphore);
  408. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  409. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_unlock_"];
  410. dispatch_semaphore_signal(semaphore);
  411. }];
  412. } else {
  413. dispatch_semaphore_signal(semaphore);
  414. }
  415. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  416. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_store_metadata_"];
  417. // UNLOCK
  418. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileId:fileId e2eToken:e2eToken onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  419. [[NCManageDatabase sharedInstance] deteleE2ETokenLockWithServerUrl:serverUrl];
  420. dispatch_semaphore_signal(semaphore);
  421. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  422. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_unlock_"];
  423. dispatch_semaphore_signal(semaphore);
  424. }];
  425. }];
  426. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  427. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_lock_"];
  428. dispatch_semaphore_signal(semaphore);
  429. }];
  430. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  431. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  432. return returnError;
  433. }
  434. - (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
  435. {
  436. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  437. __block NSError *returnError = nil;
  438. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  439. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  440. [communication setUserAgent:[CCUtility getUserAgent]];
  441. tableE2eEncryptionLock *tableLock = [[NCManageDatabase sharedInstance] getE2ETokenLockWithServerUrl:serverUrl];
  442. // LOCK
  443. [communication lockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileId:fileId e2eToken:tableLock.e2eToken onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *e2eToken, NSString *redirectedServer) {
  444. [[NCManageDatabase sharedInstance] setE2ETokenLockWithServerUrl:serverUrl fileId:fileId e2eToken:e2eToken];
  445. // UPDATA METADATA
  446. [communication updateEndToEndMetadata:[url stringByAppendingString:@"/"] fileId:fileId encryptedMetadata:metadata e2eToken:e2eToken onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *encryptedMetadata, NSString *redirectedServer) {
  447. // UNLOCK
  448. if (unlock) {
  449. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileId:fileId e2eToken:e2eToken onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  450. [[NCManageDatabase sharedInstance] deteleE2ETokenLockWithServerUrl:serverUrl];
  451. dispatch_semaphore_signal(semaphore);
  452. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  453. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_unlock_"];
  454. dispatch_semaphore_signal(semaphore);
  455. }];
  456. } else {
  457. dispatch_semaphore_signal(semaphore);
  458. }
  459. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  460. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_update_metadata_"];
  461. // UNLOCK
  462. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileId:fileId e2eToken:e2eToken onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  463. [[NCManageDatabase sharedInstance] deteleE2ETokenLockWithServerUrl:serverUrl];
  464. dispatch_semaphore_signal(semaphore);
  465. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  466. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_unlock_"];
  467. dispatch_semaphore_signal(semaphore);
  468. }];
  469. }];
  470. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  471. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_lock_"];
  472. dispatch_semaphore_signal(semaphore);
  473. }];
  474. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  475. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  476. return returnError;
  477. }
  478. - (NSError *)lockEndToEndFolderEncryptedOnServerUrl:(NSString *)serverUrl fileId:(NSString *)fileId user:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url
  479. {
  480. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  481. __block NSError *returnError = nil;
  482. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  483. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  484. [communication setUserAgent:[CCUtility getUserAgent]];
  485. tableE2eEncryptionLock *tableLock = [[NCManageDatabase sharedInstance] getE2ETokenLockWithServerUrl:serverUrl];
  486. // LOCK
  487. [communication lockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileId:fileId e2eToken:tableLock.e2eToken onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *e2eToken, NSString *redirectedServer) {
  488. [[NCManageDatabase sharedInstance] setE2ETokenLockWithServerUrl:serverUrl fileId:fileId e2eToken:e2eToken];
  489. dispatch_semaphore_signal(semaphore);
  490. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  491. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_lock_"];
  492. dispatch_semaphore_signal(semaphore);
  493. }];
  494. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  495. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  496. return returnError;
  497. }
  498. - (NSError *)unlockEndToEndFolderEncryptedOnServerUrl:(NSString *)serverUrl fileId:(NSString *)fileId e2eToken:(NSString *)e2eToken user:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url
  499. {
  500. OCCommunication *communication = [OCNetworking sharedManager].sharedOCCommunication;
  501. __block NSError *returnError = nil;
  502. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  503. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  504. [communication setUserAgent:[CCUtility getUserAgent]];
  505. // UNLOCK
  506. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileId:fileId e2eToken:e2eToken onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  507. [[NCManageDatabase sharedInstance] deteleE2ETokenLockWithServerUrl:serverUrl];
  508. dispatch_semaphore_signal(semaphore);
  509. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  510. returnError = [self getError:response error:error descriptionDefault:@"_e2e_error_unlock_"];
  511. dispatch_semaphore_signal(semaphore);
  512. }];
  513. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  514. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  515. return returnError;
  516. }
  517. - (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
  518. {
  519. tableDirectory *directory = [[NCManageDatabase sharedInstance] getTableDirectoryWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@", account, serverUrl]];
  520. NSString *metadata;
  521. NSError *error;
  522. // Enabled E2E
  523. if ([CCUtility isEndToEndEnabled:account] == NO)
  524. return [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:k_CCErrorInternalError userInfo:[NSDictionary dictionaryWithObject:NSLocalizedString(@"_e2e_error_not_enabled_", nil) forKey:NSLocalizedDescriptionKey]];
  525. // get Metadata for select updateEndToEndMetadata or storeEndToEndMetadata
  526. error = [self getEndToEndMetadata:&metadata fileId:directory.fileId user:user userID:userID password:password url:url];
  527. if (error.code != kOCErrorServerPathNotFound && error != nil) {
  528. return error;
  529. }
  530. // Rename
  531. if (fileName && fileNameNew)
  532. [[NCManageDatabase sharedInstance] renameFileE2eEncryptionWithServerUrl:serverUrl fileNameIdentifier:fileName newFileName:fileNameNew newFileNamePath:[CCUtility returnFileNamePathFromFileName:fileNameNew serverUrl:serverUrl activeUrl:url]];
  533. NSArray *tableE2eEncryption = [[NCManageDatabase sharedInstance] getE2eEncryptionsWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@", account, serverUrl]];
  534. if (!tableE2eEncryption)
  535. return [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:k_CCErrorInternalError userInfo:[NSDictionary dictionaryWithObject:NSLocalizedString(@"_e2e_error_record_not_found_", nil) forKey:NSLocalizedDescriptionKey]];
  536. NSString *e2eMetadataJSON = [[NCEndToEndMetadata sharedInstance] encoderMetadata:tableE2eEncryption privateKey:[CCUtility getEndToEndPrivateKey:account] serverUrl:serverUrl];
  537. if (!e2eMetadataJSON)
  538. return [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:k_CCErrorInternalError userInfo:[NSDictionary dictionaryWithObject:NSLocalizedString(@"_e2e_error_encode_metadata_", nil) forKey:NSLocalizedDescriptionKey]];
  539. // send Metadata
  540. if (error == nil)
  541. error = [self updateEndToEndMetadata:e2eMetadataJSON serverUrl:serverUrl fileId:directory.fileId unlock:unlock user:user userID:userID password:password url:url];
  542. else if (error.code == kOCErrorServerPathNotFound)
  543. error = [self storeEndToEndMetadata:e2eMetadataJSON serverUrl:serverUrl fileId:directory.fileId unlock:unlock user:user userID:userID password:password url:url];
  544. return error;
  545. }
  546. - (NSError *)rebuildAndSendEndToEndMetadataOnServerUrl:(NSString *)serverUrl account:(NSString *)account user:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url
  547. {
  548. NSError *error;
  549. NSString *e2eMetadataJSON;
  550. tableDirectory *directory = [[NCManageDatabase sharedInstance] getTableDirectoryWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@", account, serverUrl]];
  551. if (directory.e2eEncrypted == NO)
  552. return nil;
  553. NSArray *tableE2eEncryption = [[NCManageDatabase sharedInstance] getE2eEncryptionsWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@", account, serverUrl]];
  554. if (tableE2eEncryption) {
  555. e2eMetadataJSON = [[NCEndToEndMetadata sharedInstance] encoderMetadata:tableE2eEncryption privateKey:[CCUtility getEndToEndPrivateKey:account] serverUrl:serverUrl];
  556. if (!e2eMetadataJSON)
  557. return [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:k_CCErrorInternalError userInfo:[NSDictionary dictionaryWithObject:NSLocalizedString(@"_e2e_error_encode_metadata_", nil) forKey:NSLocalizedDescriptionKey]];
  558. error = [self updateEndToEndMetadata:e2eMetadataJSON serverUrl:serverUrl fileId:directory.fileId unlock:YES user:user userID:userID password:password url:url];
  559. } else {
  560. [self deleteEndToEndMetadataOnServerUrl:serverUrl fileId:directory.fileId unlock:YES user:user userID:userID password:password url:url];
  561. }
  562. return error;
  563. }
  564. - (NSError *)getError:(NSHTTPURLResponse *)response error:(NSError *)error descriptionDefault:(NSString *)descriptionDefault
  565. {
  566. NSInteger errorCode = response.statusCode;
  567. NSString *errorDescription = response.description;
  568. if (errorDescription == nil || errorCode == 0) {
  569. errorCode = error.code;
  570. errorDescription = error.description;
  571. if (errorDescription == nil) errorDescription = NSLocalizedString(descriptionDefault, @"");
  572. }
  573. errorDescription = [NSString stringWithFormat:@"%@ [%ld] - %@", NSLocalizedString(descriptionDefault, @""), (long)errorCode, errorDescription];
  574. if (errorDescription.length >= 250) {
  575. errorDescription = [errorDescription substringToIndex:250];
  576. errorDescription = [errorDescription stringByAppendingString:@" ..."];
  577. }
  578. return [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:errorCode userInfo:[NSDictionary dictionaryWithObject:errorDescription forKey:NSLocalizedDescriptionKey]];
  579. }
  580. @end