2017-06-09 04:23:18 +02:00
|
|
|
//
|
2018-03-02 05:30:28 +01:00
|
|
|
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
|
2017-06-09 04:23:18 +02:00
|
|
|
//
|
2016-08-23 22:38:05 +02:00
|
|
|
|
|
|
|
#import "OWSOutgoingSyncMessage.h"
|
2018-08-02 19:29:24 +02:00
|
|
|
#import "ProtoUtils.h"
|
2018-09-25 19:09:55 +02:00
|
|
|
#import <SignalCoreKit/Cryptography.h>
|
2018-09-21 21:41:10 +02:00
|
|
|
#import <SignalCoreKit/NSDate+OWS.h>
|
2018-08-01 23:13:01 +02:00
|
|
|
#import <SignalServiceKit/SignalServiceKit-Swift.h>
|
2016-08-23 22:38:05 +02:00
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
|
|
|
@implementation OWSOutgoingSyncMessage
|
|
|
|
|
2018-06-07 17:52:39 +02:00
|
|
|
- (nullable instancetype)initWithCoder:(NSCoder *)coder
|
2018-05-11 16:43:33 +02:00
|
|
|
{
|
2018-06-07 17:52:39 +02:00
|
|
|
return [super initWithCoder:coder];
|
2018-05-11 16:43:33 +02:00
|
|
|
}
|
|
|
|
|
2018-03-02 05:30:28 +01:00
|
|
|
- (instancetype)init
|
|
|
|
{
|
2018-09-20 22:10:35 +02:00
|
|
|
self = [super initOutgoingMessageWithTimestamp:[NSDate ows_millisecondTimeStamp]
|
|
|
|
inThread:nil
|
|
|
|
messageBody:nil
|
|
|
|
attachmentIds:[NSMutableArray new]
|
|
|
|
expiresInSeconds:0
|
|
|
|
expireStartedAt:0
|
|
|
|
isVoiceMessage:NO
|
|
|
|
groupMetaMessage:TSGroupMetaMessageUnspecified
|
|
|
|
quotedMessage:nil
|
|
|
|
contactShare:nil];
|
2018-02-07 18:44:09 +01:00
|
|
|
|
2018-03-02 05:30:28 +01:00
|
|
|
if (!self) {
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2017-11-15 23:53:16 +01:00
|
|
|
- (BOOL)shouldBeSaved
|
2016-09-01 21:42:51 +02:00
|
|
|
{
|
2017-11-15 23:53:16 +01:00
|
|
|
return NO;
|
2016-09-01 21:42:51 +02:00
|
|
|
}
|
|
|
|
|
2016-08-23 22:38:05 +02:00
|
|
|
- (BOOL)shouldSyncTranscript
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
2017-06-22 19:05:28 +02:00
|
|
|
// This method should not be overridden, since we want to add random padding to *every* sync message
|
2018-08-02 00:00:58 +02:00
|
|
|
- (nullable SSKProtoSyncMessage *)buildSyncMessage
|
2016-08-26 01:01:35 +02:00
|
|
|
{
|
2018-08-02 00:00:58 +02:00
|
|
|
SSKProtoSyncMessageBuilder *_Nullable builder = [self syncMessageBuilder];
|
|
|
|
if (!builder) {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2017-06-22 19:05:28 +02:00
|
|
|
// Add a random 1-512 bytes to obscure sync message type
|
|
|
|
size_t paddingBytesLength = arc4random_uniform(512) + 1;
|
|
|
|
builder.padding = [Cryptography generateRandomBytes:paddingBytesLength];
|
2017-08-04 15:33:56 +02:00
|
|
|
|
2018-08-02 00:00:58 +02:00
|
|
|
NSError *error;
|
|
|
|
SSKProtoSyncMessage *_Nullable proto = [builder buildAndReturnError:&error];
|
|
|
|
if (error || !proto) {
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSFailDebug(@"could not build protobuf: %@", error);
|
2018-08-02 00:00:58 +02:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
return proto;
|
2017-06-22 19:05:28 +02:00
|
|
|
}
|
2016-08-26 01:01:35 +02:00
|
|
|
|
2018-08-02 00:00:58 +02:00
|
|
|
- (nullable SSKProtoSyncMessageBuilder *)syncMessageBuilder
|
2017-06-22 19:05:28 +02:00
|
|
|
{
|
2018-08-08 20:29:07 +02:00
|
|
|
OWSAbstractMethod();
|
2018-08-02 00:00:58 +02:00
|
|
|
|
2018-08-01 23:13:01 +02:00
|
|
|
return [SSKProtoSyncMessageBuilder new];
|
2016-08-26 01:01:35 +02:00
|
|
|
}
|
|
|
|
|
2018-08-01 23:13:01 +02:00
|
|
|
- (nullable NSData *)buildPlainTextData:(SignalRecipient *)recipient
|
2016-08-26 01:01:35 +02:00
|
|
|
{
|
2018-08-02 00:00:58 +02:00
|
|
|
SSKProtoSyncMessage *_Nullable syncMessage = [self buildSyncMessage];
|
|
|
|
if (!syncMessage) {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2018-08-01 23:13:01 +02:00
|
|
|
SSKProtoContentBuilder *contentBuilder = [SSKProtoContentBuilder new];
|
2018-08-02 00:00:58 +02:00
|
|
|
[contentBuilder setSyncMessage:syncMessage];
|
|
|
|
|
|
|
|
NSError *error;
|
2018-08-03 20:52:55 +02:00
|
|
|
NSData *_Nullable data = [contentBuilder buildSerializedDataAndReturnError:&error];
|
2018-08-02 00:00:58 +02:00
|
|
|
if (error || !data) {
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSFailDebug(@"could not serialize protobuf: %@", error);
|
2018-08-02 00:00:58 +02:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
2016-08-26 01:01:35 +02:00
|
|
|
}
|
|
|
|
|
2016-08-23 22:38:05 +02:00
|
|
|
@end
|
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_END
|