From b89fb68a568d86e17e30ee1f990efd29a75d8a09 Mon Sep 17 00:00:00 2001 From: nielsandriesse Date: Tue, 21 Jul 2020 09:11:26 +1000 Subject: [PATCH] Clean --- SignalServiceKit/src/Loki/API/DotNetAPI.swift | 20 ++++---- .../src/Loki/API/FileServerAPI.swift | 14 +++++- .../API/Onion Requests/OnionRequestAPI.swift | 1 + .../Loki/API/Open Groups/PublicChatAPI.swift | 1 + .../Multi Device/LKDeviceLinkMessage.m | 4 +- .../Multi Device/LKUnlinkDeviceMessage.h | 2 - .../LokiSessionResetImplementation.swift | 6 +-- .../src/Messages/OWSBatchMessageProcessor.m | 1 + .../src/Messages/OWSMessageDecrypter.m | 18 +++---- .../src/Messages/OWSMessageManager.m | 2 - .../src/Messages/OWSMessageReceiver.m | 2 +- .../src/Messages/OWSMessageSender.m | 50 ------------------- 12 files changed, 41 insertions(+), 80 deletions(-) diff --git a/SignalServiceKit/src/Loki/API/DotNetAPI.swift b/SignalServiceKit/src/Loki/API/DotNetAPI.swift index ea52e330b..9883e69bd 100644 --- a/SignalServiceKit/src/Loki/API/DotNetAPI.swift +++ b/SignalServiceKit/src/Loki/API/DotNetAPI.swift @@ -26,16 +26,6 @@ public class DotNetAPI : NSObject { /// To be overridden by subclasses. internal class var authTokenCollection: String { preconditionFailure("authTokenCollection is abstract and must be overridden.") } - private static func getAuthTokenFromDatabase(for server: String) -> String? { - var result: String? = nil - storage.dbReadConnection.read { transaction in - if transaction.hasObject(forKey: server, inCollection: authTokenCollection) { - result = transaction.object(forKey: server, inCollection: authTokenCollection) as? String - } - } - return result - } - internal static func getAuthToken(for server: String) -> Promise { if let token = getAuthTokenFromDatabase(for: server) { return Promise.value(token) @@ -49,6 +39,16 @@ public class DotNetAPI : NSObject { } } + private static func getAuthTokenFromDatabase(for server: String) -> String? { + var result: String? = nil + storage.dbReadConnection.read { transaction in + if transaction.hasObject(forKey: server, inCollection: authTokenCollection) { + result = transaction.object(forKey: server, inCollection: authTokenCollection) as? String + } + } + return result + } + private static func setAuthToken(for server: String, to newValue: String, in transaction: YapDatabaseReadWriteTransaction) { transaction.setObject(newValue, forKey: server, inCollection: authTokenCollection) } diff --git a/SignalServiceKit/src/Loki/API/FileServerAPI.swift b/SignalServiceKit/src/Loki/API/FileServerAPI.swift index 5355590db..5790832c3 100644 --- a/SignalServiceKit/src/Loki/API/FileServerAPI.swift +++ b/SignalServiceKit/src/Loki/API/FileServerAPI.swift @@ -8,12 +8,14 @@ public final class FileServerAPI : DotNetAPI { private static let attachmentType = "net.app.core.oembed" public static let maxFileSize = 10_000_000 // 10 MB + @objc public static let server = "https://file.getsession.org" // MARK: Storage override internal class var authTokenCollection: String { return "LokiStorageAuthTokenCollection" } // MARK: Device Links + /// - Note: Deprecated. @objc(getDeviceLinksAssociatedWithHexEncodedPublicKey:) public static func objc_getDeviceLinks(associatedWith hexEncodedPublicKey: String) -> AnyPromise { return AnyPromise.from(getDeviceLinks(associatedWith: hexEncodedPublicKey)) @@ -21,10 +23,13 @@ public final class FileServerAPI : DotNetAPI { /// Gets the device links associated with the given hex encoded public key from the /// server and stores and returns the valid ones. + /// + /// - Note: Deprecated. public static func getDeviceLinks(associatedWith hexEncodedPublicKey: String) -> Promise> { return getDeviceLinks(associatedWith: [ hexEncodedPublicKey ]) } + /// - Note: Deprecated. @objc(getDeviceLinksAssociatedWithHexEncodedPublicKeys:) public static func objc_getDeviceLinks(associatedWith hexEncodedPublicKeys: Set) -> AnyPromise { return AnyPromise.from(getDeviceLinks(associatedWith: hexEncodedPublicKeys)) @@ -32,6 +37,8 @@ public final class FileServerAPI : DotNetAPI { /// Gets the device links associated with the given hex encoded public keys from the /// server and stores and returns the valid ones. + /// + /// - Note: Deprecated. public static func getDeviceLinks(associatedWith hexEncodedPublicKeys: Set) -> Promise> { let hexEncodedPublicKeysDescription = "[ \(hexEncodedPublicKeys.joined(separator: ", ")) ]" print("[Loki] Getting device links for: \(hexEncodedPublicKeysDescription).") @@ -87,7 +94,8 @@ public final class FileServerAPI : DotNetAPI { } }.handlingInvalidAuthTokenIfNeeded(for: server) } - + + /// - Note: Deprecated. public static func setDeviceLinks(_ deviceLinks: Set) -> Promise { print("[Loki] Updating device links.") return getAuthToken(for: server).then2 { token -> Promise in @@ -109,6 +117,8 @@ public final class FileServerAPI : DotNetAPI { } /// Adds the given device link to the user's device mapping on the server. + /// + /// - Note: Deprecated. public static func addDeviceLink(_ deviceLink: DeviceLink) -> Promise { var deviceLinks: Set = [] storage.dbReadConnection.read { transaction in @@ -121,6 +131,8 @@ public final class FileServerAPI : DotNetAPI { } /// Removes the given device link from the user's device mapping on the server. + /// + /// - Note: Deprecated. public static func removeDeviceLink(_ deviceLink: DeviceLink) -> Promise { var deviceLinks: Set = [] storage.dbReadConnection.read { transaction in diff --git a/SignalServiceKit/src/Loki/API/Onion Requests/OnionRequestAPI.swift b/SignalServiceKit/src/Loki/API/Onion Requests/OnionRequestAPI.swift index 2be0718e8..571d83296 100644 --- a/SignalServiceKit/src/Loki/API/Onion Requests/OnionRequestAPI.swift +++ b/SignalServiceKit/src/Loki/API/Onion Requests/OnionRequestAPI.swift @@ -14,6 +14,7 @@ public enum OnionRequestAPI { // MARK: Settings /// The number of snodes (including the guard snode) in a path. private static let pathSize: UInt = 3 + public static let pathCount: UInt = 2 private static var guardSnodeCount: UInt { return pathCount } // One per path diff --git a/SignalServiceKit/src/Loki/API/Open Groups/PublicChatAPI.swift b/SignalServiceKit/src/Loki/API/Open Groups/PublicChatAPI.swift index 3a2eca774..058053832 100644 --- a/SignalServiceKit/src/Loki/API/Open Groups/PublicChatAPI.swift +++ b/SignalServiceKit/src/Loki/API/Open Groups/PublicChatAPI.swift @@ -15,6 +15,7 @@ public final class PublicChatAPI : DotNetAPI { private static let maxRetryCount: UInt = 4 public static let profilePictureType = "network.loki.messenger.avatar" + @objc public static let publicChatMessageType = "network.loki.messenger.publicChat" // MARK: Convenience diff --git a/SignalServiceKit/src/Loki/Protocol/Multi Device/LKDeviceLinkMessage.m b/SignalServiceKit/src/Loki/Protocol/Multi Device/LKDeviceLinkMessage.m index 2cb8b3cbc..ddd4bf14f 100644 --- a/SignalServiceKit/src/Loki/Protocol/Multi Device/LKDeviceLinkMessage.m +++ b/SignalServiceKit/src/Loki/Protocol/Multi Device/LKDeviceLinkMessage.m @@ -38,7 +38,7 @@ SSKProtoContentBuilder *contentBuilder = [super prepareCustomContentBuilder:recipient]; NSError *error; if (self.kind == LKDeviceLinkMessageKindRequest) { - // The slave device attaches a pre key bundle with the request it sends, so that a + // The slave device attaches a pre key bundle with the request it sends so that a // session can be established with the master device. PreKeyBundle *preKeyBundle = [OWSPrimaryStorage.sharedManager generatePreKeyBundleForContact:recipient.recipientId]; SSKProtoPrekeyBundleMessageBuilder *preKeyBundleMessageBuilder = [SSKProtoPrekeyBundleMessage builderFromPreKeyBundle:preKeyBundle]; @@ -51,7 +51,7 @@ } } else { // The master device attaches its display name and profile picture URL to the device link - // authorization message, so that the slave device is in sync with these things as soon + // authorization message so that the slave device is in sync with these things as soon // as possible. id profileManager = SSKEnvironment.shared.profileManager; NSString *displayName = profileManager.localProfileName; diff --git a/SignalServiceKit/src/Loki/Protocol/Multi Device/LKUnlinkDeviceMessage.h b/SignalServiceKit/src/Loki/Protocol/Multi Device/LKUnlinkDeviceMessage.h index 39d176ea3..b9b9f9fbb 100644 --- a/SignalServiceKit/src/Loki/Protocol/Multi Device/LKUnlinkDeviceMessage.h +++ b/SignalServiceKit/src/Loki/Protocol/Multi Device/LKUnlinkDeviceMessage.h @@ -2,8 +2,6 @@ NS_ASSUME_NONNULL_BEGIN -// TODO: This is just an ephemeral message with a flag set. Not sure if it needs to be its own type. - NS_SWIFT_NAME(UnlinkDeviceMessage) @interface LKUnlinkDeviceMessage : TSOutgoingMessage diff --git a/SignalServiceKit/src/Loki/Protocol/Session Management/LokiSessionResetImplementation.swift b/SignalServiceKit/src/Loki/Protocol/Session Management/LokiSessionResetImplementation.swift index 9e3027bfc..39168c0dd 100644 --- a/SignalServiceKit/src/Loki/Protocol/Session Management/LokiSessionResetImplementation.swift +++ b/SignalServiceKit/src/Loki/Protocol/Session Management/LokiSessionResetImplementation.swift @@ -14,16 +14,16 @@ public class LokiSessionResetImplementation : NSObject, SessionResetProtocol { public func validatePreKeyForFriendRequestAcceptance(for recipientID: String, whisperMessage: CipherMessage, protocolContext: Any?) throws { guard let transaction = protocolContext as? YapDatabaseReadTransaction else { - print("[Loki] Couldn't verify friend request accepted message because an invalid transaction was provided.") + print("[Loki] Invalid transaction.") return } guard let preKeyMessage = whisperMessage as? PreKeyWhisperMessage else { return } guard let storedPreKey = storage.getPreKeyRecord(forContact: recipientID, transaction: transaction) else { - print("[Loki] Received a friend request accepted message from a public key for which no pre key bundle was created.") + print("[Loki] Missing pre key bundle.") return } guard storedPreKey.id == preKeyMessage.prekeyID else { - print("[Loki] Received a `PreKeyWhisperMessage` (friend request accepted message) from an unknown source.") + print("[Loki] Received a `PreKeyWhisperMessage` from an unknown source.") throw Error.preKeyIDsDontMatch } } diff --git a/SignalServiceKit/src/Messages/OWSBatchMessageProcessor.m b/SignalServiceKit/src/Messages/OWSBatchMessageProcessor.m index 82951ea4b..067763d21 100644 --- a/SignalServiceKit/src/Messages/OWSBatchMessageProcessor.m +++ b/SignalServiceKit/src/Messages/OWSBatchMessageProcessor.m @@ -457,6 +457,7 @@ NSString *const OWSMessageContentJobFinderExtensionGroup = @"OWSMessageContentJo } } } error:nil]; + return processedJobs; } diff --git a/SignalServiceKit/src/Messages/OWSMessageDecrypter.m b/SignalServiceKit/src/Messages/OWSMessageDecrypter.m index 84b93a54e..31001e1bf 100644 --- a/SignalServiceKit/src/Messages/OWSMessageDecrypter.m +++ b/SignalServiceKit/src/Messages/OWSMessageDecrypter.m @@ -220,11 +220,11 @@ NSError *EnsureDecryptError(NSError *_Nullable error, NSString *fallbackErrorDes [self decryptFallbackMessage:envelope envelopeData:envelopeData successBlock:^(OWSMessageDecryptResult *result) { - OWSLogDebug(@"Decrypted friend request message."); + OWSLogDebug(@"Decrypted fallback message."); successBlock(result, transaction); } failureBlock:^(NSError * _Nullable error) { - OWSLogError(@"Decrypting friend request message from: %@ failed with error: %@.", + OWSLogError(@"Decrypting fallback message from: %@ failed with error: %@.", envelopeAddress(envelope), error); failureBlock(); @@ -328,8 +328,8 @@ NSError *EnsureDecryptError(NSError *_Nullable error, NSString *fallbackErrorDes OWSAssertDebug(successBlock); OWSAssertDebug(failureBlock); - NSData *encryptedData = envelope.content; - if (!encryptedData) { + NSData *ivAndCiphertext = envelope.content; + if (ivAndCiphertext == nil) { OWSProdFail([OWSAnalyticsEvents messageManagerErrorMessageEnvelopeHasNoContent]); NSError *error = OWSErrorWithCodeDescription(OWSErrorCodeFailedToDecryptMessage, @"Envelope has no content."); return failureBlock(error); @@ -339,15 +339,15 @@ NSError *EnsureDecryptError(NSError *_Nullable error, NSString *fallbackErrorDes ECKeyPair *identityKeyPair = self.identityManager.identityKeyPair; FallBackSessionCipher *cipher = [[FallBackSessionCipher alloc] initWithRecipientPublicKey:recipientId privateKey:identityKeyPair.privateKey]; - NSData *_Nullable plaintextData = [[cipher decrypt:encryptedData] removePadding]; - if (!plaintextData) { - NSString *errorString = [NSString stringWithFormat:@"Failed to decrypt fallback message from: %@.", recipientId]; - NSError *error = OWSErrorWithCodeDescription(OWSErrorCodeFailedToDecryptMessage, errorString); + NSData *_Nullable plaintext = [[cipher decrypt:ivAndCiphertext] removePadding]; + if (plaintext == nil) { + NSString *errorDescription = [NSString stringWithFormat:@"Failed to decrypt fallback message from: %@.", recipientId]; + NSError *error = OWSErrorWithCodeDescription(OWSErrorCodeFailedToDecryptMessage, errorDescription); return failureBlock(error); } OWSMessageDecryptResult *result = [OWSMessageDecryptResult resultWithEnvelopeData:envelopeData - plaintextData:plaintextData + plaintextData:plaintext source:envelope.source sourceDevice:envelope.sourceDevice isUDMessage:NO]; diff --git a/SignalServiceKit/src/Messages/OWSMessageManager.m b/SignalServiceKit/src/Messages/OWSMessageManager.m index 0dccb4605..b7a3785bb 100644 --- a/SignalServiceKit/src/Messages/OWSMessageManager.m +++ b/SignalServiceKit/src/Messages/OWSMessageManager.m @@ -1284,10 +1284,8 @@ NS_ASSUME_NONNULL_BEGIN // ======== } - // Loki: Handle profile key update if needed [LKSessionMetaProtocol updateProfileKeyIfNeededForPublicKey:senderMasterPublicKey using:dataMessage]; - // Loki: Handle display name update if needed [LKSessionMetaProtocol updateDisplayNameIfNeededForPublicKey:senderMasterPublicKey using:dataMessage transaction:transaction]; switch (dataMessage.group.type) { diff --git a/SignalServiceKit/src/Messages/OWSMessageReceiver.m b/SignalServiceKit/src/Messages/OWSMessageReceiver.m index ac2f48f22..0637abd2d 100644 --- a/SignalServiceKit/src/Messages/OWSMessageReceiver.m +++ b/SignalServiceKit/src/Messages/OWSMessageReceiver.m @@ -398,7 +398,6 @@ NSString *const OWSMessageDecryptJobFinderExtensionGroup = @"OWSMessageProcessin successBlock:^(OWSMessageDecryptResult *result, YapDatabaseReadWriteTransaction *transaction) { OWSAssertDebug(transaction); - // Loki: Don't process any messages from ourself if ([LKSessionMetaProtocol shouldSkipMessageDecryptResult:result wrappedIn:envelope]) { dispatch_async(self.serialQueue, ^{ completion(YES); @@ -448,6 +447,7 @@ NSString *const OWSMessageDecryptJobFinderExtensionGroup = @"OWSMessageProcessin OWSSingletonAssert(); self = [super init]; + if (!self) { return self; } diff --git a/SignalServiceKit/src/Messages/OWSMessageSender.m b/SignalServiceKit/src/Messages/OWSMessageSender.m index 65f742000..08ff3b9a4 100644 --- a/SignalServiceKit/src/Messages/OWSMessageSender.m +++ b/SignalServiceKit/src/Messages/OWSMessageSender.m @@ -790,31 +790,6 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException"; [sendPromise retainUntilComplete]; } -- (void)unregisteredRecipient:(SignalRecipient *)recipient - message:(TSOutgoingMessage *)message - thread:(TSThread *)thread -{ - [LKStorage writeSyncWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { - if (thread.isGroupThread) { - // Mark as "skipped" group members who no longer have signal accounts. - [message updateWithSkippedRecipient:recipient.recipientId transaction:transaction]; - } - - if (![SignalRecipient isRegisteredRecipient:recipient.recipientId transaction:transaction]) { - return; - } - - [SignalRecipient markRecipientAsUnregistered:recipient.recipientId transaction:transaction]; - - [[TSInfoMessage userNotRegisteredMessageInThread:thread recipientId:recipient.recipientId] - saveWithTransaction:transaction]; - - // TODO: Should we deleteAllSessionsForContact here? - // If so, we'll need to avoid doing a prekey fetch every - // time we try to send a message to an unregistered user. - } error:nil]; -} - - (nullable NSArray *)deviceMessagesForMessageSend:(OWSMessageSend *)messageSend error:(NSError **)errorHandle { @@ -1243,27 +1218,6 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException"; }); }; - void (^handle404)(void) = ^{ - OWSLogWarn(@"Unregistered recipient: %@.", recipient.uniqueId); - - dispatch_async(OWSDispatch.sendingQueue, ^{ - if (![messageSend.message isKindOfClass:[OWSOutgoingSyncMessage class]]) { - TSThread *_Nullable thread = messageSend.thread; - OWSAssertDebug(thread); - [self unregisteredRecipient:recipient message:message thread:thread]; - } - - NSError *error = OWSErrorMakeNoSuchSignalRecipientError(); - // No need to retry if the recipient is not registered. - [error setIsRetryable:NO]; - // If one member of a group deletes their account, - // the group should ignore errors when trying to send - // messages to this ex-member. - [error setShouldBeIgnoredForGroups:YES]; - messageSend.failure(error); - }); - }; - switch (statusCode) { case 0: { // Loki NSError *error; @@ -1285,10 +1239,6 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException"; [error setIsRetryable:NO]; return messageSend.failure(error); } - case 404: { - handle404(); - return; - } default: retrySend(); break;