NCNetworkingEndToEnd.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. @end