NCNetworkingEndToEnd.m 43 KB

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