pull/246/head
Ryan ZHAO 5 years ago
commit caaf396010

@ -60,9 +60,18 @@ public final class SessionManagementProtocol : NSObject {
// MARK: - Sending // MARK: - Sending
@objc(isSessionRequiredForMessage:recipientID:transaction:)
public static func isSessionRequired(for message: TSOutgoingMessage, recipientID: String, transaction: YapDatabaseReadWriteTransaction) -> Bool {
if SharedSenderKeysImplementation.shared.isClosedGroup(recipientID) {
return false
} else {
return !shouldUseFallbackEncryption(for: message, recipientID: recipientID, transaction: transaction)
}
}
@objc(shouldUseFallbackEncryptionForMessage:recipientID:transaction:) @objc(shouldUseFallbackEncryptionForMessage:recipientID:transaction:)
public static func shouldUseFallbackEncryption(for message: TSOutgoingMessage, recipientID: String, transaction: YapDatabaseReadWriteTransaction) -> Bool { public static func shouldUseFallbackEncryption(for message: TSOutgoingMessage, recipientID: String, transaction: YapDatabaseReadWriteTransaction) -> Bool {
if SharedSenderKeysImplementation.shared.isClosedGroup(recipientID) { return true } // We don't actually use fallback encryption but this indicates that we don't need a session if SharedSenderKeysImplementation.shared.isClosedGroup(recipientID) { return false }
else if message is SessionRequestMessage { return true } else if message is SessionRequestMessage { return true }
else if let message = message as? DeviceLinkMessage, message.kind == .request { return true } else if let message = message as? DeviceLinkMessage, message.kind == .request { return true }
else if message is OWSOutgoingNullMessage { return false } else if message is OWSOutgoingNullMessage { return false }

@ -1077,7 +1077,10 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
[self.dbConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) { [self.dbConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
isUserLinkedDevice = [LKDatabaseUtilities isUserLinkedDevice:targetPublicKey in:transaction]; isUserLinkedDevice = [LKDatabaseUtilities isUserLinkedDevice:targetPublicKey in:transaction];
}]; }];
if ([targetPublicKey isEqual:userPublicKey]) { BOOL isSSKBasedClosedGroup = [messageSend.thread isKindOfClass:TSGroupThread.class] && ((TSGroupThread *)messageSend.thread).usesSharedSenderKeys;
if (isSSKBasedClosedGroup) {
[LKLogger print:[NSString stringWithFormat:@"[Loki] Sending %@ to SSK based closed group.", message.class]];
} else if ([targetPublicKey isEqual:userPublicKey]) {
[LKLogger print:[NSString stringWithFormat:@"[Loki] Sending %@ to self.", message.class]]; [LKLogger print:[NSString stringWithFormat:@"[Loki] Sending %@ to self.", message.class]];
} else if (isUserLinkedDevice) { } else if (isUserLinkedDevice) {
[LKLogger print:[NSString stringWithFormat:@"[Loki] Sending %@ to %@ (one of the current user's linked devices).", message.class, recipient.recipientId]]; [LKLogger print:[NSString stringWithFormat:@"[Loki] Sending %@ to %@ (one of the current user's linked devices).", message.class, recipient.recipientId]];
@ -1086,14 +1089,17 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
} }
NSDictionary *signalMessageInfo = deviceMessages.firstObject; NSDictionary *signalMessageInfo = deviceMessages.firstObject;
SSKProtoEnvelopeType type = ((NSNumber *)signalMessageInfo[@"type"]).integerValue; SSKProtoEnvelopeType type = ((NSNumber *)signalMessageInfo[@"type"]).integerValue;
if ([messageSend.thread isKindOfClass:TSGroupThread.class] && ((TSGroupThread *)messageSend.thread).usesSharedSenderKeys) { if (isSSKBasedClosedGroup) {
type = SSKProtoEnvelopeTypeClosedGroupCiphertext; type = SSKProtoEnvelopeTypeClosedGroupCiphertext;
} }
uint64_t timestamp = message.timestamp; uint64_t timestamp = message.timestamp;
NSString *senderID = (type == SSKProtoEnvelopeTypeUnidentifiedSender) ? @"" : userPublicKey; NSString *senderID;
if ([messageSend.thread isKindOfClass:TSGroupThread.class] && ((TSGroupThread *)messageSend.thread).usesSharedSenderKeys) { if (isSSKBasedClosedGroup) {
senderID = [LKGroupUtilities getDecodedGroupID:((TSGroupThread *)messageSend.thread).groupModel.groupId]; senderID = [LKGroupUtilities getDecodedGroupID:((TSGroupThread *)messageSend.thread).groupModel.groupId];
} else if (type == SSKProtoEnvelopeTypeUnidentifiedSender) {
senderID = @"";
} else { } else {
senderID = userPublicKey;
[LKLogger print:@"[Loki] Non-UD send"]; [LKLogger print:@"[Loki] Non-UD send"];
} }
uint32_t senderDeviceID = type == SSKProtoEnvelopeTypeUnidentifiedSender ? 0 : OWSDevicePrimaryDeviceId; uint32_t senderDeviceID = type == SSKProtoEnvelopeTypeUnidentifiedSender ? 0 : OWSDevicePrimaryDeviceId;
@ -1385,7 +1391,7 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
@try { @try {
__block BOOL isSessionRequired; __block BOOL isSessionRequired;
[LKStorage writeSyncWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { [LKStorage writeSyncWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
isSessionRequired = ![LKSessionManagementProtocol shouldUseFallbackEncryptionForMessage:messageSend.message recipientID:recipientID transaction:transaction]; isSessionRequired = [LKSessionManagementProtocol isSessionRequiredForMessage:messageSend.message recipientID:recipientID transaction:transaction];
} error:nil]; } error:nil];
if (isSessionRequired) { if (isSessionRequired) {
BOOL hasSession = [self throws_ensureRecipientHasSessionForMessageSend:messageSend recipientID:recipientID deviceId:@(OWSDevicePrimaryDeviceId)]; BOOL hasSession = [self throws_ensureRecipientHasSessionForMessageSend:messageSend recipientID:recipientID deviceId:@(OWSDevicePrimaryDeviceId)];

Loading…
Cancel
Save