GroupPhotoSender.m 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2013-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. #import "GroupPhotoSender.h"
  21. #import "Conversation.h"
  22. #import "Utils.h"
  23. #import "NaClCrypto.h"
  24. #import "MyIdentityStore.h"
  25. #import "GroupSetPhotoMessage.h"
  26. #import "MessageQueue.h"
  27. #import "NSString+Hex.h"
  28. #import "ActivityIndicatorProxy.h"
  29. #import "SSLCAHelper.h"
  30. #import "GroupSetPhotoMessage.h"
  31. #import "GroupDeletePhotoMessage.h"
  32. #import "Contact.h"
  33. #import "BlobUtil.h"
  34. #import "ThreemaError.h"
  35. #ifdef DEBUG
  36. static const DDLogLevel ddLogLevel = DDLogLevelVerbose;
  37. #else
  38. static const DDLogLevel ddLogLevel = DDLogLevelWarning;
  39. #endif
  40. @implementation GroupPhotoSender {
  41. Conversation *conversation;
  42. Contact *toMember;
  43. NSData *boxImageData;
  44. NSData *nonce;
  45. NSData *encryptionKey;
  46. NSMutableData *receivedData;
  47. NSURLConnection *uploadConnection;
  48. void(^onCompletion)(void);
  49. void(^onError)(NSError *error);
  50. }
  51. - (void)startWithImageData:(NSData *)imageData inConversation:(Conversation*)_conversation toMember:(Contact*)_toMember onCompletion:(void (^)(void))_onCompletion onError:(void (^)(NSError *))_onError {
  52. conversation = _conversation;
  53. toMember = _toMember;
  54. onCompletion = _onCompletion;
  55. onError = _onError;
  56. if (imageData != nil) { // New image
  57. /* Generate random symmetric key and encrypt */
  58. encryptionKey = [[NaClCrypto sharedCrypto] randomBytes:kBlobKeyLen];
  59. boxImageData = [[NaClCrypto sharedCrypto] symmetricEncryptData:imageData withKey:encryptionKey nonce:[NSData dataWithBytesNoCopy:kNonce_1 length:sizeof(kNonce_1) freeWhenDone:NO]];
  60. [self startUpload];
  61. } else { // Image was removed
  62. [self sendDeletePhotoMessage];
  63. }
  64. }
  65. - (void)startUpload {
  66. [ActivityIndicatorProxy startActivity];
  67. NSURL *blobUploadUrl = [BlobUtil urlForBlobUpload];
  68. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:blobUploadUrl cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:kBlobUploadTimeout];
  69. request.HTTPMethod = @"POST";
  70. NSString *boundary = @"---------------------------Boundary_Line";
  71. NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
  72. [request addValue:contentType forHTTPHeaderField: @"Content-Type"];
  73. NSMutableData *body = [NSMutableData dataWithCapacity:boxImageData.length + 1024];
  74. [body appendData:[[NSString stringWithFormat:@"--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
  75. [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"blob\"; filename=\"blob.bin\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
  76. [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
  77. [body appendData:boxImageData];
  78. [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
  79. [request setHTTPBody:body];
  80. [request addValue:[NSString stringWithFormat:@"%lu", (unsigned long)[body length]] forHTTPHeaderField:@"Content-Length"];
  81. receivedData = [NSMutableData data];
  82. uploadConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];
  83. [uploadConnection scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
  84. [uploadConnection start];
  85. }
  86. - (void)uploadCompletedWithBlobId:(NSData*)blobId {
  87. if (toMember != nil) {
  88. /* send to the specified member only */
  89. [self sendSetPhotoMessageToMember:toMember withBlobId:blobId];
  90. } else {
  91. /* send to each group member */
  92. for (Contact *member in conversation.members) {
  93. [self sendSetPhotoMessageToMember:member withBlobId:blobId];
  94. }
  95. }
  96. dispatch_async(dispatch_get_main_queue(), ^{
  97. onCompletion();
  98. });
  99. }
  100. - (void)sendDeletePhotoMessage {
  101. if (toMember != nil) {
  102. /* send to the specified member only */
  103. [self sendDeletePhotoMessageToMember:toMember];
  104. } else {
  105. /* send to each group member */
  106. for (Contact *member in conversation.members) {
  107. [self sendDeletePhotoMessageToMember:member];
  108. }
  109. }
  110. dispatch_async(dispatch_get_main_queue(), ^{
  111. onCompletion();
  112. });
  113. }
  114. - (void)sendSetPhotoMessageToMember:(Contact*)member withBlobId:(NSData*)blobId {
  115. DDLogVerbose(@"Sending group photo message to %@", member.identity);
  116. GroupSetPhotoMessage *msg = [[GroupSetPhotoMessage alloc] init];
  117. msg.blobId = blobId;
  118. msg.size = (uint32_t)boxImageData.length;
  119. msg.encryptionKey = encryptionKey;
  120. msg.groupId = conversation.groupId;
  121. msg.groupCreator = [MyIdentityStore sharedMyIdentityStore].identity;
  122. msg.toIdentity = member.identity;
  123. [[MessageQueue sharedMessageQueue] enqueue:msg];
  124. }
  125. - (void)sendDeletePhotoMessageToMember:(Contact *)member {
  126. DDLogVerbose(@"Sending group delete photo message to %@", member.identity);
  127. GroupDeletePhotoMessage *msg = [[GroupDeletePhotoMessage alloc] init];
  128. msg.groupId = conversation.groupId;
  129. msg.groupCreator = [MyIdentityStore sharedMyIdentityStore].identity;
  130. msg.toIdentity = member.identity;
  131. [[MessageQueue sharedMessageQueue] enqueue:msg];
  132. }
  133. #pragma mark - URL connection delegate
  134. - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
  135. DDLogVerbose(@"Request succeeded - received %lu bytes", (unsigned long)[receivedData length]);
  136. [ActivityIndicatorProxy stopActivity];
  137. NSString *blobIdHex = [[NSString alloc] initWithData:receivedData encoding:NSASCIIStringEncoding];
  138. NSData *blobId = [blobIdHex decodeHex];
  139. if (blobId.length != kBlobIdLen) {
  140. DDLogError(@"Got invalid blob ID from server: %@", blobId);
  141. [self connection:connection didFailWithError:[ThreemaError threemaError:@"Got invalid blob ID from server"]];
  142. return;
  143. }
  144. DDLogVerbose(@"Blob ID: %@", blobId);
  145. [self uploadCompletedWithBlobId:blobId];
  146. }
  147. - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
  148. DDLogError(@"Connection failed - error %@ %@",
  149. [error localizedDescription],
  150. [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
  151. [ActivityIndicatorProxy stopActivity];
  152. onError(error);
  153. }
  154. /* this method ensures that HTTP errors get treated like connection failures etc. as well */
  155. - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
  156. [receivedData setLength:0];
  157. NSInteger statusCode = [((NSHTTPURLResponse *)response) statusCode];
  158. if (statusCode >= 400)
  159. {
  160. [connection cancel]; // stop connecting; no more delegate messages
  161. NSDictionary *errorInfo = [NSDictionary dictionaryWithObject:[NSString stringWithFormat:
  162. NSLocalizedString(@"Server returned status code %d",@""),
  163. statusCode]
  164. forKey:NSLocalizedDescriptionKey];
  165. NSError *statusError = [NSError errorWithDomain:NSURLErrorDomain
  166. code:statusCode
  167. userInfo:errorInfo];
  168. [self connection:connection didFailWithError:statusError];
  169. }
  170. }
  171. - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
  172. [receivedData appendData:data];
  173. }
  174. - (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
  175. return [SSLCAHelper connection:connection canAuthenticateAgainstProtectionSpace:protectionSpace];
  176. }
  177. - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
  178. [SSLCAHelper connection:connection didReceiveAuthenticationChallenge:challenge];
  179. }
  180. - (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse {
  181. return nil;
  182. }
  183. @end