send null message when syncing verification state

// FREEBIE
This commit is contained in:
Michael Kirk 2017-06-22 12:12:24 -04:00
parent 99cd8fc27d
commit 35ee92f38f
8 changed files with 113 additions and 8 deletions

View file

@ -19,6 +19,8 @@ NS_ASSUME_NONNULL_BEGIN
// This is a clunky name, but we want to differentiate it from `recipientIdentifier` inherited from `TSOutgoingMessage`
@property (nonatomic, readonly) NSString *verificationForRecipientId;
@property (nonatomic, readonly) size_t paddingBytesLength;
@end
NS_ASSUME_NONNULL_END

View file

@ -68,6 +68,9 @@ NS_ASSUME_NONNULL_BEGIN
_verificationState = verificationState;
_identityKey = identityKey;
_verificationForRecipientId = verificationForRecipientId;
// Add 1-512 bytes of random padding bytes.
_paddingBytesLength = arc4random_uniform(512) + 1;
return self;
}
@ -110,11 +113,10 @@ NS_ASSUME_NONNULL_BEGIN
return OWSSignalServiceProtosVerifiedStateUnverified;
}
}();
// Add 1-512 bytes of random padding bytes.
size_t paddingLengthBytes = arc4random_uniform(512) + 1;
syncMessageBuilder.padding = [Cryptography generateRandomBytes:paddingLengthBytes];
OWSAssert(self.paddingBytesLength != 0);
syncMessageBuilder.padding = [Cryptography generateRandomBytes:self.paddingBytesLength];
return [syncMessageBuilder build];
}

View file

@ -455,8 +455,6 @@ NSString *const kTSOutgoingMessageSentRecipientAll = @"kTSOutgoingMessageSentRec
return [[self dataMessageBuilder] build];
}
// For legacy message this is a serialized DataMessage.
// For modern messages, e.g. Sync and Call messages, this is a serialized Contact
- (NSData *)buildPlainTextData
{
OWSSignalServiceProtosContentBuilder *contentBuilder = [OWSSignalServiceProtosContentBuilder new];

View file

@ -47,7 +47,7 @@ extern const NSUInteger kIdentityKeyLength;
- (nullable OWSRecipientIdentity *)untrustedIdentityForSendingToRecipientId:(NSString *)recipientId;
// Will try to send a sync message with all verification states.
- (void)syncAllVerificationStates;
//- (void)syncAllVerificationStates;
- (void)processIncomingSyncMessage:(OWSSignalServiceProtosVerified *)verified;

View file

@ -7,6 +7,7 @@
#import "NotificationsProtocol.h"
#import "OWSMessageSender.h"
#import "OWSRecipientIdentity.h"
#import "OWSOutgoingNullMessage.h"
#import "OWSVerificationStateChangeMessage.h"
#import "OWSVerificationStateSyncMessage.h"
#import "TSAccountManager.h"
@ -550,6 +551,20 @@ NSString *const kNSNotificationName_IdentityStateDidChange = @"kNSNotificationNa
OWSAssert(message.verificationForRecipientId.length > 0);
OWSAssert([NSThread isMainThread]);
TSContactThread *contactThread = [TSContactThread getOrCreateThreadWithContactId:message.verificationForRecipientId];
// Send null message to appear as though we're sending a normal message to cover the sync messsage sent
// subsequently
OWSOutgoingNullMessage *nullMessage = [[OWSOutgoingNullMessage alloc] initWithContactThread:contactThread
verificationStateSyncMessage:message];
[self.messageSender sendMessage:nullMessage
success:^{
DDLogInfo(@"%@ Successfully sent verification state NullMessage", self.tag);
}
failure:^(NSError * _Nonnull error) {
DDLogError(@"%@ Failed to send verification state NullMessage with error: %@", self.tag, error);
}];
[self.messageSender sendMessage:message
success:^{
DDLogInfo(@"%@ Successfully sent verification state sync message", self.tag);

View file

@ -0,0 +1,19 @@
//
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
//
#import "TSOutgoingMessage.h"
NS_ASSUME_NONNULL_BEGIN
@class OWSVerificationStateSyncMessage;
@class TSContactThread;
@interface OWSOutgoingNullMessage : TSOutgoingMessage
- (instancetype)initWithContactThread:(TSContactThread *)contactThread
verificationStateSyncMessage:(OWSVerificationStateSyncMessage *)verificationStateSyncMessage;
@end
NS_ASSUME_NONNULL_END

View file

@ -0,0 +1,67 @@
//
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
//
#import "OWSOutgoingNullMessage.h"
#import "OWSSignalServiceProtos.pb.h"
#import "Cryptography.h"
#import "OWSVerificationStateSyncMessage.h"
#import "NSDate+millisecondTimeStamp.h"
#import "TSContactThread.h"
NS_ASSUME_NONNULL_BEGIN
@interface OWSOutgoingNullMessage ()
@property (nonatomic, readonly) OWSVerificationStateSyncMessage *verificationStateSyncMessage;
@end
@implementation OWSOutgoingNullMessage
- (instancetype)initWithContactThread:(TSContactThread *)contactThread
verificationStateSyncMessage:(OWSVerificationStateSyncMessage *)verificationStateSyncMessage
{
self = [super initWithTimestamp:[NSDate ows_millisecondTimeStamp]
inThread:contactThread];
if (!self) {
return self;
}
_verificationStateSyncMessage = verificationStateSyncMessage;
return self;
}
#pragma mark - override TSOutgoingMessage
- (NSData *)buildPlainTextData
{
OWSSignalServiceProtosContentBuilder *contentBuilder = [OWSSignalServiceProtosContentBuilder new];
OWSSignalServiceProtosNullMessageBuilder *nullMessageBuilder = [OWSSignalServiceProtosNullMessageBuilder new];
// TODO padding size.
NSUInteger contentLength = self.verificationStateSyncMessage.buildPlainTextData.length;
contentLength -= self.verificationStateSyncMessage.paddingBytesLength;
OWSAssert(contentLength > 0)
// Add 1-512 bytes of random padding bytes.
contentLength += arc4random_uniform(512) + 1;
nullMessageBuilder.padding = [Cryptography generateRandomBytes:contentLength];
contentBuilder.nullMessage = [nullMessageBuilder build];
return [contentBuilder build].data;
}
- (void)saveWithTransaction:(YapDatabaseReadWriteTransaction *)transaction
{
// No-op as we don't want to actually display this as an outgoing message in our thread.
return;
}
@end
NS_ASSUME_NONNULL_END

View file

@ -171,6 +171,8 @@ NS_ASSUME_NONNULL_BEGIN
return [NSString stringWithFormat:@"<DataMessage: %@ />", [self descriptionForDataMessage:content.dataMessage]];
} else if (content.hasCallMessage) {
return [NSString stringWithFormat:@"<CallMessage: %@ />", content.callMessage];
} else if (content.hasNullMessage) {
return [NSString stringWithFormat:@"<NullMessage: %@ />", content.nullMessage];
} else {
OWSAssert(NO);
return @"UnknownContent";