From b9d39f8758c6f048854b8452fdc25d110ca9dbe9 Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Mon, 6 May 2019 16:12:49 +1000 Subject: [PATCH 1/2] Fix public key validation bug --- SignalServiceKit/src/Loki/ECKeyPair.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/SignalServiceKit/src/Loki/ECKeyPair.swift b/SignalServiceKit/src/Loki/ECKeyPair.swift index a2581a5e5..c9ed0453e 100644 --- a/SignalServiceKit/src/Loki/ECKeyPair.swift +++ b/SignalServiceKit/src/Loki/ECKeyPair.swift @@ -14,8 +14,8 @@ public extension ECKeyPair { // Check that it's a valid hexadecimal encoding let allowedCharacters = CharacterSet(charactersIn: "0123456789ABCDEF") guard candidate.uppercased().unicodeScalars.allSatisfy({ allowedCharacters.contains($0) }) else { return false } - // Check that it has either length 33 and a leading "05" or length 32 - guard (candidate.count == 33 && candidate.hasPrefix("05")) || candidate.count == 32 else { return false } + // Check that it has either length 66 and a leading "05" or length 64 + guard (candidate.count == 66 && candidate.hasPrefix("05")) || candidate.count == 64 else { return false } // It appears to be a valid public key return true } From ee2361b5cc3b7e5a43d61a882e896dc1c3b79acb Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Mon, 6 May 2019 16:13:32 +1000 Subject: [PATCH 2/2] Hook up Loki messaging API --- .../OnboardingKeyPairViewController.swift | 11 ------- .../src/Loki/LokiMessagingAPI.swift | 33 +++++++------------ .../src/Messages/OWSMessageSender.m | 6 ++-- .../Network/API/Requests/OWSRequestFactory.m | 8 ++--- 4 files changed, 18 insertions(+), 40 deletions(-) diff --git a/Signal/src/ViewControllers/Registration/OnboardingKeyPairViewController.swift b/Signal/src/ViewControllers/Registration/OnboardingKeyPairViewController.swift index 8a515ef74..3aafac6d4 100644 --- a/Signal/src/ViewControllers/Registration/OnboardingKeyPairViewController.swift +++ b/Signal/src/ViewControllers/Registration/OnboardingKeyPairViewController.swift @@ -96,17 +96,6 @@ final class OnboardingKeyPairViewController : OnboardingBaseViewController { setUpViewHierarchy() handleModeChanged() // Perform initial update updateKeyPair() - // Test - // ================ - let _ = LokiMessagingAPI.retrieveAllMessages().done { result in - print(result.task.originalRequest!) - print(result.task.response!) - } - let _ = LokiMessagingAPI.sendTestMessage().done { result in - print(result.task.originalRequest!) - print(result.task.response!) - } - // ================ } private func setUpViewHierarchy() { diff --git a/SignalServiceKit/src/Loki/LokiMessagingAPI.swift b/SignalServiceKit/src/Loki/LokiMessagingAPI.swift index f3704b4c4..f3a23402a 100644 --- a/SignalServiceKit/src/Loki/LokiMessagingAPI.swift +++ b/SignalServiceKit/src/Loki/LokiMessagingAPI.swift @@ -1,6 +1,6 @@ import PromiseKit -public struct LokiMessagingAPI { +@objc public final class LokiMessagingAPI : NSObject { private static var baseURL: String { return textSecureServerURL } private static var port: String { return "8080" } @@ -9,39 +9,30 @@ public struct LokiMessagingAPI { // MARK: Types private enum Method : String { case retrieveAllMessages = "retrieve" - case send = "store" + case sendMessage = "store" } - public typealias Response = TSNetworkManager.NetworkManagerResult + public typealias RawResponse = TSNetworkManager.NetworkManagerResult // MARK: Lifecycle - private init() { } + override private init() { } // MARK: API - private static func invoke(_ method: Method, parameters: [String:String] = [:]) -> Promise { + private static func invoke(_ method: Method, parameters: [String:String] = [:]) -> (request: TSRequest, promise: Promise) { let url = URL(string: "\(baseURL):\(port)/\(apiVersion)/storage_rpc")! let request = TSRequest(url: url, method: "POST", parameters: [ "method" : method.rawValue, "params" : parameters ]) - return TSNetworkManager.shared().makePromise(request: request) + return (request, TSNetworkManager.shared().makePromise(request: request)) } - - public static func sendTestMessage() -> Promise { - let hour = 60 * 60 * 1000 - let ttl = String(4 * 24 * hour) - let parameters = [ - "pubKey" : "0371e72be8dd42ff77105e474a3ac26a503d017fb4562409c639eaf5965f5b31", // TODO: Receiver's public key - "ttl" : ttl, - "nonce" : "AAAAAAAA5rs=", // TODO: Proof of work - "timestamp" : "1556259498201", // TODO: Message send time - "data" : "CAESvgMKA1BVVBIPL2FwaS92MS9tZXNzYWdlGqIDCGUSQjA1MDM3MWU3MmJlOGRkNDJmZjc3MTA1ZTQ3NGEzYWMyNmE1MDNkMDE3ZmI0NTYyNDA5YzYzOWVhZjU5NjVmNWIzYzgBKK3QrcKlLULQAlxclJTbzKeQjJPfPlvo0VdoNw+O6kmpAUAKz2Mmz0YDHnhIsFgdWlBIoudqxVDu7swq5Z4cUqMfcQ5Z0b03/dVjkmFYo79Hzv7wkmRlPsfqAOVLBgV06sLVl+C5d8EmDtfH+k2iT62HnD8fub8tIxHn2l0MCefB4kO8tbA4dl/n/IXlvRAFS7OPJiq3jLyykyZkauAW7SVdDBAO6exJlNyOHTgSaHF924V3a/s3BK0useVMbzJSun9cx68Jm3WGERMFqrd75X70PN933zUSHedBAmMFW1Mvecko1G854tfNPZllP7OO/o+6XrQm8hMoe0Zo3POelrXwRdX88jp9VSEio/Yugq9MMcBuMsU5G0ePK5ZJMNfGLwExGSLY4br3sYpJz5yO7slpq2GgPuO6t9hWwIfzWynvNIfVtDxBkLVSV5XZU7720p/KP6kqZWCGHyCsAQ==" // TODO: Encrypted content - ] - return invoke(.send, parameters: parameters) + + @objc public static func sendMessage(_ message: [String:String]) -> TSRequest { + return invoke(.sendMessage, parameters: message).request } - public static func retrieveAllMessages() -> Promise { + public static func retrieveAllMessages() -> Promise { let parameters = [ "pubKey" : OWSIdentityManager.shared().identityKeyPair()!.hexEncodedPublicKey, - "lastHash" : "" + "lastHash" : "" // TODO: Implement ] - return invoke(.retrieveAllMessages, parameters: parameters) + return invoke(.retrieveAllMessages, parameters: parameters).promise } } diff --git a/SignalServiceKit/src/Messages/OWSMessageSender.m b/SignalServiceKit/src/Messages/OWSMessageSender.m index 7902ee348..420224692 100644 --- a/SignalServiceKit/src/Messages/OWSMessageSender.m +++ b/SignalServiceKit/src/Messages/OWSMessageSender.m @@ -929,7 +929,7 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException"; NSString *destination = message[@"destination"]; NSString *data = message[@"content"]; - NSString *_Nullable nonce = [ProofOfWork calculateWithData:data pubKey:destination timestamp:timestamp.unsignedIntegerValue ttl:ttl.integerValue]; + NSString *nonce = [ProofOfWork calculateWithData:data pubKey:destination timestamp:timestamp.unsignedIntegerValue ttl:ttl.integerValue]; // Return our timestamp along with the nonce // These will help us identify which nonce belongs to which message @@ -943,7 +943,7 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException"; [promises addObject:promise]; } - // Wait for all the PoW Calculations to finish + // Wait for all the PoW calculations to finish return PMKWhen(promises); } @@ -1126,7 +1126,7 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException"; return messageSend.failure(error); } - // TODO: Update message here to show the pow cog icon + // TODO: Update message here to show the POW cog icon // Loki: Calculate the proof of work for each device message NSNumber *ttl = [NSNumber numberWithInteger:@(4 * 24 * 60 * 60)]; diff --git a/SignalServiceKit/src/Network/API/Requests/OWSRequestFactory.m b/SignalServiceKit/src/Network/API/Requests/OWSRequestFactory.m index a13551ad4..704d95d81 100644 --- a/SignalServiceKit/src/Network/API/Requests/OWSRequestFactory.m +++ b/SignalServiceKit/src/Network/API/Requests/OWSRequestFactory.m @@ -366,7 +366,7 @@ NS_ASSUME_NONNULL_BEGIN // Params for our message server lokiMessage[@"pubKey"] = message[@"destination"]; lokiMessage[@"data"] = message[@"content"]; - lokiMessage[@"ttl"] = ttl; + lokiMessage[@"ttl"] = [ttl stringValue]; NSDictionary *_Nullable nonce = [self getNonceFromArray:nonceArray forMessage:message]; if (nonce) { @@ -380,7 +380,6 @@ NS_ASSUME_NONNULL_BEGIN return modifiedMessages; } - + (NSDictionary *_Nullable)getNonceFromArray:(NSArray *)nonceArray forMessage:(NSDictionary *)message { NSPredicate *predicate = [NSPredicate predicateWithFormat:@"destination == %@ AND deviceId == %d", message[@"destination"], message[@"destinationDeviceId"]]; NSArray *filtered = [nonceArray filteredArrayUsingPredicate:predicate]; @@ -405,9 +404,8 @@ NS_ASSUME_NONNULL_BEGIN // Loki: Just send the first message NSString *path = [textSecureMessagesAPI stringByAppendingString:recipientId]; NSDictionary *parameters = [lokiMessages objectAtIndex:0]; - - TSRequest *request = [TSRequest requestWithUrl:[NSURL URLWithString:path] method:@"PUT" parameters:parameters]; - return request; + + return [LokiMessagingAPI sendMessage:parameters]; } + (TSRequest *)submitMessageRequestWithRecipient:(NSString *)recipientId