2017-07-25 00:05:26 +02:00
|
|
|
//
|
2018-01-11 15:56:38 +01:00
|
|
|
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
|
2017-07-25 00:05:26 +02:00
|
|
|
//
|
|
|
|
|
|
|
|
#import "OWSMessageReceiver.h"
|
2017-11-29 21:27:19 +01:00
|
|
|
#import "AppContext.h"
|
2018-01-30 17:34:00 +01:00
|
|
|
#import "AppReadiness.h"
|
2017-09-20 17:48:37 +02:00
|
|
|
#import "NSArray+OWS.h"
|
2018-04-17 21:23:05 +02:00
|
|
|
#import "NotificationsProtocol.h"
|
2017-12-15 17:55:10 +01:00
|
|
|
#import "OWSBackgroundTask.h"
|
2017-09-13 18:49:19 +02:00
|
|
|
#import "OWSBatchMessageProcessor.h"
|
2017-09-14 17:00:30 +02:00
|
|
|
#import "OWSMessageDecrypter.h"
|
2018-03-05 15:30:58 +01:00
|
|
|
#import "OWSPrimaryStorage.h"
|
2017-09-21 20:36:52 +02:00
|
|
|
#import "OWSQueues.h"
|
2017-07-25 00:05:26 +02:00
|
|
|
#import "OWSSignalServiceProtos.pb.h"
|
2017-12-19 04:56:02 +01:00
|
|
|
#import "OWSStorage.h"
|
2017-07-25 00:05:26 +02:00
|
|
|
#import "TSDatabaseView.h"
|
2018-04-17 21:23:05 +02:00
|
|
|
#import "TSErrorMessage.h"
|
2017-07-25 00:05:26 +02:00
|
|
|
#import "TSYapDatabaseObject.h"
|
2018-04-17 21:23:05 +02:00
|
|
|
#import "TextSecureKitEnv.h"
|
2017-09-13 21:35:33 +02:00
|
|
|
#import "Threading.h"
|
2017-12-12 16:31:05 +01:00
|
|
|
#import <YapDatabase/YapDatabaseAutoView.h>
|
2017-07-25 00:05:26 +02:00
|
|
|
#import <YapDatabase/YapDatabaseConnection.h>
|
|
|
|
#import <YapDatabase/YapDatabaseTransaction.h>
|
2017-12-12 16:31:05 +01:00
|
|
|
#import <YapDatabase/YapDatabaseViewTypes.h>
|
2017-07-25 00:05:26 +02:00
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
2017-09-20 17:48:37 +02:00
|
|
|
@interface OWSMessageDecryptJob : TSYapDatabaseObject
|
2017-07-25 00:05:26 +02:00
|
|
|
|
|
|
|
@property (nonatomic, readonly) NSDate *createdAt;
|
2017-09-27 17:30:23 +02:00
|
|
|
@property (nonatomic, readonly) NSData *envelopeData;
|
2017-07-25 00:05:26 +02:00
|
|
|
|
|
|
|
- (instancetype)initWithEnvelope:(OWSSignalServiceProtosEnvelope *)envelope NS_DESIGNATED_INITIALIZER;
|
2017-09-27 17:30:23 +02:00
|
|
|
- (nullable instancetype)initWithCoder:(NSCoder *)coder NS_DESIGNATED_INITIALIZER;
|
2017-11-15 19:15:48 +01:00
|
|
|
- (instancetype)initWithUniqueId:(NSString *_Nullable)uniqueId NS_UNAVAILABLE;
|
2017-07-26 18:04:50 +02:00
|
|
|
- (OWSSignalServiceProtosEnvelope *)envelopeProto;
|
2017-07-25 00:05:26 +02:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
2017-09-13 18:14:22 +02:00
|
|
|
#pragma mark -
|
|
|
|
|
2017-09-20 17:48:37 +02:00
|
|
|
@implementation OWSMessageDecryptJob
|
|
|
|
|
|
|
|
+ (NSString *)collection
|
|
|
|
{
|
|
|
|
return @"OWSMessageProcessingJob";
|
|
|
|
}
|
2017-07-25 00:05:26 +02:00
|
|
|
|
|
|
|
- (instancetype)initWithEnvelope:(OWSSignalServiceProtosEnvelope *)envelope
|
|
|
|
{
|
2017-07-26 18:04:50 +02:00
|
|
|
OWSAssert(envelope);
|
|
|
|
|
2017-07-25 00:05:26 +02:00
|
|
|
self = [super initWithUniqueId:[NSUUID new].UUIDString];
|
|
|
|
if (!self) {
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
_envelopeData = envelope.data;
|
|
|
|
_createdAt = [NSDate new];
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2017-09-27 17:30:23 +02:00
|
|
|
- (nullable instancetype)initWithCoder:(NSCoder *)coder
|
|
|
|
{
|
|
|
|
return [super initWithCoder:coder];
|
|
|
|
}
|
|
|
|
|
2017-07-25 00:05:26 +02:00
|
|
|
- (OWSSignalServiceProtosEnvelope *)envelopeProto
|
|
|
|
{
|
|
|
|
return [OWSSignalServiceProtosEnvelope parseFromData:self.envelopeData];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
#pragma mark - Finder
|
|
|
|
|
2017-09-27 18:32:03 +02:00
|
|
|
NSString *const OWSMessageDecryptJobFinderExtensionName = @"OWSMessageProcessingJobFinderExtensionName2";
|
|
|
|
NSString *const OWSMessageDecryptJobFinderExtensionGroup = @"OWSMessageProcessingJobFinderExtensionGroup2";
|
2017-07-25 00:05:26 +02:00
|
|
|
|
2017-09-20 17:48:37 +02:00
|
|
|
@interface OWSMessageDecryptJobFinder : NSObject
|
2017-07-25 00:05:26 +02:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
2017-09-13 18:14:22 +02:00
|
|
|
#pragma mark -
|
|
|
|
|
2017-09-20 17:48:37 +02:00
|
|
|
@interface OWSMessageDecryptJobFinder ()
|
2017-07-25 00:05:26 +02:00
|
|
|
|
|
|
|
@property (nonatomic, readonly) YapDatabaseConnection *dbConnection;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2017-09-13 18:14:22 +02:00
|
|
|
#pragma mark -
|
|
|
|
|
2017-09-20 17:48:37 +02:00
|
|
|
@implementation OWSMessageDecryptJobFinder
|
2017-07-25 00:05:26 +02:00
|
|
|
|
|
|
|
- (instancetype)initWithDBConnection:(YapDatabaseConnection *)dbConnection
|
|
|
|
{
|
|
|
|
OWSSingletonAssert();
|
|
|
|
|
|
|
|
self = [super init];
|
|
|
|
if (!self) {
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
_dbConnection = dbConnection;
|
|
|
|
|
2017-09-27 18:40:36 +02:00
|
|
|
[OWSMessageDecryptJobFinder registerLegacyClasses];
|
2017-09-27 17:30:23 +02:00
|
|
|
|
2017-07-25 00:05:26 +02:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2017-09-21 19:46:30 +02:00
|
|
|
- (OWSMessageDecryptJob *_Nullable)nextJob
|
2017-07-25 00:05:26 +02:00
|
|
|
{
|
2017-09-21 19:46:30 +02:00
|
|
|
__block OWSMessageDecryptJob *_Nullable job = nil;
|
2017-07-25 00:05:26 +02:00
|
|
|
[self.dbConnection readWithBlock:^(YapDatabaseReadTransaction *_Nonnull transaction) {
|
2017-09-20 17:48:37 +02:00
|
|
|
YapDatabaseViewTransaction *viewTransaction = [transaction ext:OWSMessageDecryptJobFinderExtensionName];
|
2017-07-25 00:05:26 +02:00
|
|
|
OWSAssert(viewTransaction != nil);
|
2017-09-21 19:46:30 +02:00
|
|
|
job = [viewTransaction firstObjectInGroup:OWSMessageDecryptJobFinderExtensionGroup];
|
2017-07-25 00:05:26 +02:00
|
|
|
}];
|
|
|
|
|
2017-09-21 19:46:30 +02:00
|
|
|
return job;
|
2017-07-25 00:05:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)addJobForEnvelope:(OWSSignalServiceProtosEnvelope *)envelope
|
|
|
|
{
|
|
|
|
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull transaction) {
|
2018-02-14 20:06:39 +01:00
|
|
|
OWSMessageDecryptJob *job = [[OWSMessageDecryptJob alloc] initWithEnvelope:envelope];
|
|
|
|
[job saveWithTransaction:transaction];
|
2017-07-25 00:05:26 +02:00
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2017-09-21 19:46:30 +02:00
|
|
|
- (void)removeJobWithId:(NSString *)uniqueId
|
2017-07-25 00:05:26 +02:00
|
|
|
{
|
|
|
|
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull transaction) {
|
2017-09-21 19:46:30 +02:00
|
|
|
[transaction removeObjectForKey:uniqueId inCollection:[OWSMessageDecryptJob collection]];
|
2017-07-25 00:05:26 +02:00
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2017-09-13 18:14:22 +02:00
|
|
|
+ (YapDatabaseView *)databaseExtension
|
2017-07-25 00:05:26 +02:00
|
|
|
{
|
|
|
|
YapDatabaseViewSorting *sorting =
|
|
|
|
[YapDatabaseViewSorting withObjectBlock:^NSComparisonResult(YapDatabaseReadTransaction *transaction,
|
|
|
|
NSString *group,
|
|
|
|
NSString *collection1,
|
|
|
|
NSString *key1,
|
|
|
|
id object1,
|
|
|
|
NSString *collection2,
|
|
|
|
NSString *key2,
|
|
|
|
id object2) {
|
|
|
|
|
2017-09-20 17:48:37 +02:00
|
|
|
if (![object1 isKindOfClass:[OWSMessageDecryptJob class]]) {
|
2017-07-26 18:04:50 +02:00
|
|
|
OWSFail(@"Unexpected object: %@ in collection: %@", [object1 class], collection1);
|
|
|
|
return NSOrderedSame;
|
2017-07-25 00:05:26 +02:00
|
|
|
}
|
2017-09-20 17:48:37 +02:00
|
|
|
OWSMessageDecryptJob *job1 = (OWSMessageDecryptJob *)object1;
|
2017-07-25 00:05:26 +02:00
|
|
|
|
2017-09-20 17:48:37 +02:00
|
|
|
if (![object2 isKindOfClass:[OWSMessageDecryptJob class]]) {
|
2017-07-26 18:04:50 +02:00
|
|
|
OWSFail(@"Unexpected object: %@ in collection: %@", [object2 class], collection2);
|
|
|
|
return NSOrderedSame;
|
2017-07-25 00:05:26 +02:00
|
|
|
}
|
2017-09-20 17:48:37 +02:00
|
|
|
OWSMessageDecryptJob *job2 = (OWSMessageDecryptJob *)object2;
|
2017-07-25 00:05:26 +02:00
|
|
|
|
|
|
|
return [job1.createdAt compare:job2.createdAt];
|
|
|
|
}];
|
|
|
|
|
|
|
|
YapDatabaseViewGrouping *grouping =
|
|
|
|
[YapDatabaseViewGrouping withObjectBlock:^NSString *_Nullable(YapDatabaseReadTransaction *_Nonnull transaction,
|
|
|
|
NSString *_Nonnull collection,
|
|
|
|
NSString *_Nonnull key,
|
|
|
|
id _Nonnull object) {
|
2017-09-20 17:48:37 +02:00
|
|
|
if (![object isKindOfClass:[OWSMessageDecryptJob class]]) {
|
2017-07-26 18:04:50 +02:00
|
|
|
OWSFail(@"Unexpected object: %@ in collection: %@", object, collection);
|
|
|
|
return nil;
|
2017-07-25 00:05:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Arbitrary string - all in the same group. We're only using the view for sorting.
|
2017-09-20 17:48:37 +02:00
|
|
|
return OWSMessageDecryptJobFinderExtensionGroup;
|
2017-07-25 00:05:26 +02:00
|
|
|
}];
|
|
|
|
|
|
|
|
YapDatabaseViewOptions *options = [YapDatabaseViewOptions new];
|
|
|
|
options.allowedCollections =
|
2017-09-20 17:48:37 +02:00
|
|
|
[[YapWhitelistBlacklist alloc] initWithWhitelist:[NSSet setWithObject:[OWSMessageDecryptJob collection]]];
|
2017-07-25 00:05:26 +02:00
|
|
|
|
2017-12-12 16:31:05 +01:00
|
|
|
return [[YapDatabaseAutoView alloc] initWithGrouping:grouping sorting:sorting versionTag:@"1" options:options];
|
2017-07-25 00:05:26 +02:00
|
|
|
}
|
|
|
|
|
2017-09-27 18:40:36 +02:00
|
|
|
+ (void)registerLegacyClasses
|
|
|
|
{
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
dispatch_once(&onceToken, ^{
|
2017-09-27 18:59:38 +02:00
|
|
|
// We've renamed OWSMessageProcessingJob to OWSMessageDecryptJob.
|
2017-09-27 18:40:36 +02:00
|
|
|
[NSKeyedUnarchiver setClass:[OWSMessageDecryptJob class] forClassName:[OWSMessageDecryptJob collection]];
|
|
|
|
});
|
|
|
|
}
|
2017-07-25 00:05:26 +02:00
|
|
|
|
2018-01-26 21:53:26 +01:00
|
|
|
+ (void)asyncRegisterDatabaseExtension:(OWSStorage *)storage
|
2017-07-25 00:05:26 +02:00
|
|
|
{
|
2017-09-27 18:40:36 +02:00
|
|
|
[self registerLegacyClasses];
|
|
|
|
|
2018-01-11 15:56:38 +01:00
|
|
|
YapDatabaseView *existingView = [storage registeredExtension:OWSMessageDecryptJobFinderExtensionName];
|
2017-07-25 00:05:26 +02:00
|
|
|
if (existingView) {
|
2017-09-20 17:48:37 +02:00
|
|
|
OWSFail(@"%@ was already initialized.", OWSMessageDecryptJobFinderExtensionName);
|
2017-07-25 00:05:26 +02:00
|
|
|
// already initialized
|
|
|
|
return;
|
|
|
|
}
|
2018-01-26 21:53:26 +01:00
|
|
|
[storage asyncRegisterExtension:[self databaseExtension] withName:OWSMessageDecryptJobFinderExtensionName];
|
2017-07-25 00:05:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
#pragma mark - Queue Processing
|
|
|
|
|
2017-09-20 17:48:37 +02:00
|
|
|
@interface OWSMessageDecryptQueue : NSObject
|
2017-07-25 00:05:26 +02:00
|
|
|
|
2017-09-14 17:00:30 +02:00
|
|
|
@property (nonatomic, readonly) OWSMessageDecrypter *messageDecrypter;
|
2017-09-13 18:49:19 +02:00
|
|
|
@property (nonatomic, readonly) OWSBatchMessageProcessor *batchMessageProcessor;
|
2017-09-20 17:48:37 +02:00
|
|
|
@property (nonatomic, readonly) OWSMessageDecryptJobFinder *finder;
|
2017-07-28 21:57:06 +02:00
|
|
|
@property (nonatomic) BOOL isDrainingQueue;
|
2017-07-25 00:05:26 +02:00
|
|
|
|
2017-09-14 17:00:30 +02:00
|
|
|
- (instancetype)initWithMessageDecrypter:(OWSMessageDecrypter *)messageDecrypter
|
|
|
|
batchMessageProcessor:(OWSBatchMessageProcessor *)batchMessageProcessor
|
|
|
|
finder:(OWSMessageDecryptJobFinder *)finder NS_DESIGNATED_INITIALIZER;
|
2017-07-25 00:05:26 +02:00
|
|
|
- (instancetype)init NS_UNAVAILABLE;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2017-09-13 18:14:22 +02:00
|
|
|
#pragma mark -
|
|
|
|
|
2017-09-20 17:48:37 +02:00
|
|
|
@implementation OWSMessageDecryptQueue
|
2017-07-25 00:05:26 +02:00
|
|
|
|
2017-09-14 17:00:30 +02:00
|
|
|
- (instancetype)initWithMessageDecrypter:(OWSMessageDecrypter *)messageDecrypter
|
|
|
|
batchMessageProcessor:(OWSBatchMessageProcessor *)batchMessageProcessor
|
|
|
|
finder:(OWSMessageDecryptJobFinder *)finder
|
2017-07-25 00:05:26 +02:00
|
|
|
{
|
|
|
|
OWSSingletonAssert();
|
|
|
|
|
|
|
|
self = [super init];
|
|
|
|
if (!self) {
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2017-09-14 17:00:30 +02:00
|
|
|
_messageDecrypter = messageDecrypter;
|
2017-09-13 18:49:19 +02:00
|
|
|
_batchMessageProcessor = batchMessageProcessor;
|
2017-07-25 00:05:26 +02:00
|
|
|
_finder = finder;
|
2017-07-28 21:57:06 +02:00
|
|
|
_isDrainingQueue = NO;
|
2017-07-25 00:05:26 +02:00
|
|
|
|
2018-02-14 22:06:47 +01:00
|
|
|
[AppReadiness runNowOrWhenAppIsReady:^{
|
|
|
|
[self drainQueue];
|
|
|
|
}];
|
2017-09-13 21:35:33 +02:00
|
|
|
|
2017-07-25 00:05:26 +02:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - instance methods
|
|
|
|
|
2017-09-21 20:36:52 +02:00
|
|
|
- (dispatch_queue_t)serialQueue
|
|
|
|
{
|
|
|
|
static dispatch_queue_t queue = nil;
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
dispatch_once(&onceToken, ^{
|
|
|
|
queue = dispatch_queue_create("org.whispersystems.message.decrypt", DISPATCH_QUEUE_SERIAL);
|
|
|
|
});
|
|
|
|
return queue;
|
|
|
|
}
|
|
|
|
|
2017-07-25 00:05:26 +02:00
|
|
|
- (void)enqueueEnvelopeForProcessing:(OWSSignalServiceProtosEnvelope *)envelope
|
|
|
|
{
|
|
|
|
[self.finder addJobForEnvelope:envelope];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)drainQueue
|
|
|
|
{
|
2018-02-14 22:06:47 +01:00
|
|
|
OWSAssert(AppReadiness.isAppReady);
|
|
|
|
|
2017-11-29 21:27:19 +01:00
|
|
|
// Don't decrypt messages in app extensions.
|
|
|
|
if (!CurrentAppContext().isMainApp) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-21 20:36:52 +02:00
|
|
|
dispatch_async(self.serialQueue, ^{
|
2017-09-13 21:35:33 +02:00
|
|
|
if (self.isDrainingQueue) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
self.isDrainingQueue = YES;
|
|
|
|
|
|
|
|
[self drainQueueWorkStep];
|
|
|
|
});
|
2017-07-28 21:57:06 +02:00
|
|
|
}
|
2017-07-25 00:05:26 +02:00
|
|
|
|
2017-07-28 21:57:06 +02:00
|
|
|
- (void)drainQueueWorkStep
|
|
|
|
{
|
2017-09-21 20:36:52 +02:00
|
|
|
AssertOnDispatchQueue(self.serialQueue);
|
2017-07-28 21:57:06 +02:00
|
|
|
|
2017-09-21 19:46:30 +02:00
|
|
|
OWSMessageDecryptJob *_Nullable job = [self.finder nextJob];
|
|
|
|
if (!job) {
|
2017-07-28 21:57:06 +02:00
|
|
|
self.isDrainingQueue = NO;
|
2017-11-08 20:04:51 +01:00
|
|
|
DDLogVerbose(@"%@ Queue is drained.", self.logTag);
|
2017-07-28 21:57:06 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-15 19:03:03 +01:00
|
|
|
__block OWSBackgroundTask *backgroundTask = [OWSBackgroundTask backgroundTaskWithLabelStr:__PRETTY_FUNCTION__];
|
2017-12-15 17:55:10 +01:00
|
|
|
|
2017-09-21 19:46:30 +02:00
|
|
|
[self processJob:job
|
|
|
|
completion:^(BOOL success) {
|
|
|
|
[self.finder removeJobWithId:job.uniqueId];
|
|
|
|
DDLogVerbose(@"%@ %@ job. %lu jobs left.",
|
2017-11-08 20:04:51 +01:00
|
|
|
self.logTag,
|
2017-09-21 19:46:30 +02:00
|
|
|
success ? @"decrypted" : @"failed to decrypt",
|
|
|
|
(unsigned long)[OWSMessageDecryptJob numberOfKeysInCollection]);
|
|
|
|
[self drainQueueWorkStep];
|
2017-12-15 17:55:10 +01:00
|
|
|
backgroundTask = nil;
|
2017-09-21 19:46:30 +02:00
|
|
|
}];
|
2017-09-14 00:03:02 +02:00
|
|
|
}
|
|
|
|
|
2017-09-21 19:46:30 +02:00
|
|
|
- (void)processJob:(OWSMessageDecryptJob *)job completion:(void (^)(BOOL))completion
|
2017-09-14 00:03:02 +02:00
|
|
|
{
|
2017-09-21 20:36:52 +02:00
|
|
|
AssertOnDispatchQueue(self.serialQueue);
|
2017-09-21 19:46:30 +02:00
|
|
|
OWSAssert(job);
|
2017-09-14 00:03:02 +02:00
|
|
|
|
2018-04-16 20:48:29 +02:00
|
|
|
OWSSignalServiceProtosEnvelope *_Nullable envelope = nil;
|
|
|
|
@try {
|
|
|
|
envelope = job.envelopeProto;
|
|
|
|
} @catch (NSException *exception) {
|
|
|
|
OWSProdLogAndFail(@"%@ Could not parse proto: %@", self.logTag, exception.debugDescription);
|
|
|
|
// TODO: Add analytics.
|
2018-04-17 21:23:05 +02:00
|
|
|
|
|
|
|
[[OWSPrimaryStorage.sharedManager newDatabaseConnection]
|
|
|
|
readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
|
|
|
|
TSErrorMessage *errorMessage = [TSErrorMessage corruptedMessageInUnknownThread];
|
|
|
|
[[TextSecureKitEnv sharedEnv].notificationsManager notifyUserForThreadlessErrorMessage:errorMessage
|
|
|
|
transaction:transaction];
|
|
|
|
}];
|
|
|
|
|
2018-04-16 20:48:29 +02:00
|
|
|
dispatch_async(self.serialQueue, ^{
|
|
|
|
completion(NO);
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-21 20:36:52 +02:00
|
|
|
[self.messageDecrypter decryptEnvelope:envelope
|
2018-01-30 21:05:04 +01:00
|
|
|
successBlock:^(NSData *_Nullable plaintextData, YapDatabaseReadWriteTransaction *transaction) {
|
|
|
|
OWSAssert(transaction);
|
|
|
|
|
|
|
|
// We persist the decrypted envelope data in the same transaction within which
|
|
|
|
// it was decrypted to prevent data loss. If the new job isn't persisted,
|
|
|
|
// the session state side effects of its decryption are also rolled back.
|
|
|
|
[self.batchMessageProcessor enqueueEnvelopeData:job.envelopeData
|
|
|
|
plaintextData:plaintextData
|
|
|
|
transaction:transaction];
|
2017-09-21 19:46:30 +02:00
|
|
|
|
2017-09-21 20:36:52 +02:00
|
|
|
dispatch_async(self.serialQueue, ^{
|
|
|
|
completion(YES);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
failureBlock:^{
|
|
|
|
dispatch_async(self.serialQueue, ^{
|
|
|
|
completion(NO);
|
|
|
|
});
|
|
|
|
}];
|
2017-07-25 00:05:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
#pragma mark - OWSMessageReceiver
|
|
|
|
|
|
|
|
@interface OWSMessageReceiver ()
|
|
|
|
|
2017-09-20 17:48:37 +02:00
|
|
|
@property (nonatomic, readonly) OWSMessageDecryptQueue *processingQueue;
|
2017-07-25 00:05:26 +02:00
|
|
|
@property (nonatomic, readonly) YapDatabaseConnection *dbConnection;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2017-09-13 18:14:22 +02:00
|
|
|
#pragma mark -
|
|
|
|
|
2017-07-25 00:05:26 +02:00
|
|
|
@implementation OWSMessageReceiver
|
|
|
|
|
|
|
|
- (instancetype)initWithDBConnection:(YapDatabaseConnection *)dbConnection
|
2017-09-14 17:00:30 +02:00
|
|
|
messageDecrypter:(OWSMessageDecrypter *)messageDecrypter
|
2017-09-13 18:49:19 +02:00
|
|
|
batchMessageProcessor:(OWSBatchMessageProcessor *)batchMessageProcessor
|
2017-07-25 00:05:26 +02:00
|
|
|
{
|
|
|
|
OWSSingletonAssert();
|
|
|
|
|
|
|
|
self = [super init];
|
|
|
|
if (!self) {
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2017-09-20 17:48:37 +02:00
|
|
|
OWSMessageDecryptJobFinder *finder = [[OWSMessageDecryptJobFinder alloc] initWithDBConnection:dbConnection];
|
|
|
|
OWSMessageDecryptQueue *processingQueue =
|
2017-09-14 17:00:30 +02:00
|
|
|
[[OWSMessageDecryptQueue alloc] initWithMessageDecrypter:messageDecrypter
|
|
|
|
batchMessageProcessor:batchMessageProcessor
|
|
|
|
finder:finder];
|
2017-07-25 00:05:26 +02:00
|
|
|
|
|
|
|
_processingQueue = processingQueue;
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (instancetype)initDefault
|
|
|
|
{
|
|
|
|
// For concurrency coherency we use the same dbConnection to persist and read the unprocessed envelopes
|
2018-03-05 15:30:58 +01:00
|
|
|
YapDatabaseConnection *dbConnection = [[OWSPrimaryStorage sharedManager] newDatabaseConnection];
|
2017-09-14 17:00:30 +02:00
|
|
|
OWSMessageDecrypter *messageDecrypter = [OWSMessageDecrypter sharedManager];
|
2017-09-13 18:49:19 +02:00
|
|
|
OWSBatchMessageProcessor *batchMessageProcessor = [OWSBatchMessageProcessor sharedInstance];
|
2017-07-25 00:05:26 +02:00
|
|
|
|
2017-09-13 18:49:19 +02:00
|
|
|
return [self initWithDBConnection:dbConnection
|
2017-09-14 17:00:30 +02:00
|
|
|
messageDecrypter:messageDecrypter
|
2017-09-13 18:49:19 +02:00
|
|
|
batchMessageProcessor:batchMessageProcessor];
|
2017-07-25 00:05:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
+ (instancetype)sharedInstance
|
|
|
|
{
|
|
|
|
static OWSMessageReceiver *sharedInstance;
|
|
|
|
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
dispatch_once(&onceToken, ^{
|
|
|
|
sharedInstance = [[self alloc] initDefault];
|
|
|
|
});
|
|
|
|
|
|
|
|
return sharedInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - class methods
|
|
|
|
|
2018-01-26 21:53:26 +01:00
|
|
|
+ (void)asyncRegisterDatabaseExtension:(OWSStorage *)storage
|
2017-07-25 00:05:26 +02:00
|
|
|
{
|
2018-01-26 21:53:26 +01:00
|
|
|
[OWSMessageDecryptJobFinder asyncRegisterDatabaseExtension:storage];
|
2017-07-25 00:05:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - instance methods
|
|
|
|
|
2017-07-28 23:04:52 +02:00
|
|
|
- (void)handleAnyUnprocessedEnvelopesAsync
|
2017-07-28 22:05:48 +02:00
|
|
|
{
|
2017-09-13 21:35:33 +02:00
|
|
|
[self.processingQueue drainQueue];
|
2017-07-28 22:05:48 +02:00
|
|
|
}
|
|
|
|
|
2017-07-25 00:05:26 +02:00
|
|
|
- (void)handleReceivedEnvelope:(OWSSignalServiceProtosEnvelope *)envelope
|
|
|
|
{
|
2017-07-26 20:48:49 +02:00
|
|
|
// Drop any too-large messages on the floor. Well behaving clients should never send them.
|
|
|
|
NSUInteger kMaxEnvelopeByteCount = 250 * 1024;
|
|
|
|
if (envelope.serializedSize > kMaxEnvelopeByteCount) {
|
2017-07-27 18:29:05 +02:00
|
|
|
OWSProdError([OWSAnalyticsEvents messageReceiverErrorOversizeMessage]);
|
2017-07-26 20:48:49 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Take note of any messages larger than we expect, but still process them.
|
|
|
|
// This likely indicates a misbehaving sending client.
|
|
|
|
NSUInteger kLargeEnvelopeWarningByteCount = 25 * 1024;
|
|
|
|
if (envelope.serializedSize > kLargeEnvelopeWarningByteCount) {
|
2017-07-27 18:29:05 +02:00
|
|
|
OWSProdError([OWSAnalyticsEvents messageReceiverErrorLargeMessage]);
|
2017-07-26 20:48:49 +02:00
|
|
|
}
|
|
|
|
|
2017-07-25 00:05:26 +02:00
|
|
|
[self.processingQueue enqueueEnvelopeForProcessing:envelope];
|
|
|
|
[self.processingQueue drainQueue];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_END
|