FileMessageDecoder.m 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2015-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 "FileMessageDecoder.h"
  21. #import "FileMessageKeys.h"
  22. #import "NSString+Hex.h"
  23. #import "FileMessage.h"
  24. #import "EntityManager.h"
  25. #import "PinnedHTTPSURLLoader.h"
  26. #import "BlobUtil.h"
  27. #import "ThreemaError.h"
  28. #import "NaClCrypto.h"
  29. #ifdef DEBUG
  30. static const DDLogLevel ddLogLevel = DDLogLevelVerbose;
  31. #else
  32. static const DDLogLevel ddLogLevel = DDLogLevelWarning;
  33. #endif
  34. typedef void (^CompletionBlock)(BaseMessage *message);
  35. typedef void (^ErrorBlock)(NSError *err);
  36. @interface FileMessageDecoder ()
  37. @property Conversation *conversation;
  38. @property EntityManager *entityManager;
  39. @property AbstractMessage *boxMessage;
  40. @property NSDictionary *json;
  41. @property NSData *jsonData;
  42. @property (copy) CompletionBlock onCompletion;
  43. @property (copy) ErrorBlock onError;
  44. @end
  45. @implementation FileMessageDecoder
  46. + (void)decodeMessageFromBox:(BoxFileMessage *)message forConversation:conversation onCompletion:(void (^)(BaseMessage *))onCompletion onError:(void (^)(NSError *))onError {
  47. FileMessageDecoder *decoder = [FileMessageDecoder fileMessageDecoderOnCompletion:onCompletion onError:onError conversation:conversation];
  48. [decoder decodeMessageFromBox:message];
  49. }
  50. + (void)decodeGroupMessageFromBox:(GroupFileMessage *)message forConversation:conversation onCompletion:(void (^)(BaseMessage *))onCompletion onError:(void (^)(NSError *))onError {
  51. FileMessageDecoder *decoder = [FileMessageDecoder fileMessageDecoderOnCompletion:onCompletion onError:onError conversation:conversation];
  52. [decoder decodeGroupMessageFromBox:message];
  53. }
  54. + (NSString *)decodeFilenameFromBox:(BoxFileMessage *)message {
  55. FileMessageDecoder *decoder = [[FileMessageDecoder alloc] init];
  56. if ([decoder prepareJson:message.jsonData] == NO) {
  57. return nil;
  58. }
  59. return [decoder.json objectForKey: JSON_FILE_KEY_FILENAME];
  60. }
  61. + (NSString *)decodeGroupFilenameFromBox:(GroupFileMessage *)message {
  62. FileMessageDecoder *decoder = [[FileMessageDecoder alloc] init];
  63. if ([decoder prepareJson:message.jsonData] == NO) {
  64. return nil;
  65. }
  66. return [decoder.json objectForKey: JSON_FILE_KEY_FILENAME];
  67. }
  68. + (NSString *)decodeFileCaptionFromBox:(BoxFileMessage *)message {
  69. FileMessageDecoder *decoder = [[FileMessageDecoder alloc] init];
  70. if ([decoder prepareJson:message.jsonData] == NO) {
  71. return nil;
  72. }
  73. return [decoder.json objectForKey: JSON_FILE_KEY_DESCRIPTION];
  74. }
  75. + (NSString *)decodeGroupFileCaptionFromBox:(GroupFileMessage *)message {
  76. FileMessageDecoder *decoder = [[FileMessageDecoder alloc] init];
  77. if ([decoder prepareJson:message.jsonData] == NO) {
  78. return nil;
  79. }
  80. return [decoder.json objectForKey: JSON_FILE_KEY_DESCRIPTION];
  81. }
  82. #pragma mark - private
  83. + (instancetype)fileMessageDecoderOnCompletion:(void(^)(BaseMessage *message))onCompletion onError:(void(^)(NSError *err))onError conversation:(Conversation *)conversation {
  84. FileMessageDecoder *decoder = [[FileMessageDecoder alloc] init];
  85. decoder.onCompletion = onCompletion;
  86. decoder.onError = onError;
  87. decoder.entityManager = [[EntityManager alloc] init];
  88. decoder.conversation = conversation;
  89. return decoder;
  90. }
  91. - (void)decodeMessageFromBox:(BoxFileMessage *)message {
  92. if ([self prepareJson:message.jsonData] == NO) {
  93. return;
  94. }
  95. [self handleMessage:message];
  96. }
  97. - (void)decodeGroupMessageFromBox:(GroupFileMessage *)message {
  98. if ([self prepareJson:message.jsonData] == NO) {
  99. return;
  100. }
  101. [self handleMessage:message];
  102. }
  103. - (void)handleMessage:(AbstractMessage *)message {
  104. _boxMessage = message;
  105. [self fetchThumbnail];
  106. }
  107. - (BOOL)prepareJson:(NSData *)data {
  108. NSError *error;
  109. _json = (NSDictionary *)[NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
  110. if (_json == nil) {
  111. DDLogError(@"Error parsing json %@, %@", error, [error userInfo]);
  112. if (_onError != nil) {
  113. _onError([ThreemaError threemaError:@"Error parsing file message json data"]);
  114. }
  115. return NO;
  116. }
  117. _jsonData = data;
  118. return YES;
  119. }
  120. - (void)fetchThumbnail {
  121. NSString *thumbnailBlobHex = [_json objectForKey: JSON_FILE_KEY_THUMBNAIL_BLOB];
  122. if (thumbnailBlobHex) {
  123. NSData *thumbnailId = [thumbnailBlobHex decodeHex];
  124. NSURLRequest *request = [BlobUtil urlRequestForBlobId:thumbnailId];
  125. PinnedHTTPSURLLoader *thumbnailLoader = [[PinnedHTTPSURLLoader alloc] init];
  126. [thumbnailLoader startWithURLRequest:request onCompletion:^(NSData *data) {
  127. NSString *encryptionKeyHex = [_json objectForKey: JSON_FILE_KEY_ENCRYPTION_KEY];
  128. NSData *encryptionKey = [encryptionKeyHex decodeHex];
  129. /* Decrypt the box */
  130. NSData *decodedData = [[NaClCrypto sharedCrypto] symmetricDecryptData:data withKey:encryptionKey nonce:[NSData dataWithBytesNoCopy:kNonce_2 length:sizeof(kNonce_2) freeWhenDone:NO]];
  131. if (decodedData == nil) {
  132. DDLogError(@"Could not decode thumbnail data");
  133. }
  134. [self createDBMessageWithThumbnail:decodedData error:nil];
  135. } onError:^(NSError *error) {
  136. [self createDBMessageWithThumbnail:nil error:error];
  137. }];
  138. } else {
  139. [self createDBMessageWithThumbnail:nil error:nil];
  140. }
  141. }
  142. - (void)createDBMessageWithThumbnail:(NSData *)thumbnailData error:(NSError *)error {
  143. __block FileMessage *fileMessage;
  144. [_entityManager performSyncBlockAndSafe:^{
  145. fileMessage = [_entityManager.entityCreator fileMessageFromBox:_boxMessage];
  146. fileMessage.conversation = _conversation;
  147. NSString *blobHex = [_json objectForKey: JSON_FILE_KEY_FILE_BLOB];
  148. fileMessage.blobId = [blobHex decodeHex];
  149. NSString *thumbnailBlobHex = [_json objectForKey: JSON_FILE_KEY_THUMBNAIL_BLOB];
  150. if (thumbnailBlobHex) {
  151. fileMessage.blobThumbnailId = [thumbnailBlobHex decodeHex];
  152. }
  153. NSString *encryptionKeyHex = [_json objectForKey: JSON_FILE_KEY_ENCRYPTION_KEY];
  154. fileMessage.encryptionKey = [encryptionKeyHex decodeHex];
  155. fileMessage.mimeType = [_json objectForKey: JSON_FILE_KEY_MIMETYPE];
  156. fileMessage.fileSize = [_json objectForKey: JSON_FILE_KEY_FILESIZE];
  157. NSNumber *type = [_json objectForKey: JSON_FILE_KEY_TYPE];
  158. if (type == nil) {
  159. type = [_json objectForKey: JSON_FILE_KEY_TYPE_DEPRECATED];
  160. if (type == nil) {
  161. fileMessage.type = @0;
  162. }
  163. } else {
  164. fileMessage.type = type;
  165. }
  166. NSString *filename = [_json objectForKey: JSON_FILE_KEY_FILENAME];
  167. if (filename) {
  168. fileMessage.fileName = filename;
  169. }
  170. NSString *caption = [_json objectForKey: JSON_FILE_KEY_DESCRIPTION];
  171. if (caption) {
  172. fileMessage.caption = caption;
  173. }
  174. fileMessage.json = [[NSString alloc] initWithData:_jsonData encoding:NSUTF8StringEncoding];
  175. if (thumbnailData) {
  176. ImageData *thumbnail = [_entityManager.entityCreator imageData];
  177. thumbnail.data = thumbnailData;
  178. // load image to determine size
  179. UIImage *thumbnailImage = [UIImage imageWithData:thumbnailData];
  180. thumbnail.width = [NSNumber numberWithInt:thumbnailImage.size.width];
  181. thumbnail.height = [NSNumber numberWithInt:thumbnailImage.size.height];
  182. fileMessage.thumbnail = thumbnail;
  183. }
  184. }];
  185. if (error) {
  186. _onError(error);
  187. } else {
  188. _onCompletion(fileMessage);
  189. }
  190. }
  191. @end