From dedfea78da196cea0f8bdb7f30695af256f38f60 Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Fri, 20 Jul 2018 09:25:53 -0600 Subject: [PATCH] callback handlers for remote attestation --- .../src/Contacts/ContactDiscoveryService.h | 5 +- .../src/Contacts/ContactDiscoveryService.m | 48 ++++++++++++++----- 2 files changed, 41 insertions(+), 12 deletions(-) diff --git a/SignalServiceKit/src/Contacts/ContactDiscoveryService.h b/SignalServiceKit/src/Contacts/ContactDiscoveryService.h index 929e7bfcb..79cb40e60 100644 --- a/SignalServiceKit/src/Contacts/ContactDiscoveryService.h +++ b/SignalServiceKit/src/Contacts/ContactDiscoveryService.h @@ -4,6 +4,8 @@ NS_ASSUME_NONNULL_BEGIN +@class RemoteAttestation; + @interface ContactDiscoveryService : NSObject - (instancetype)init NS_UNAVAILABLE; @@ -11,7 +13,8 @@ NS_ASSUME_NONNULL_BEGIN + (instancetype)sharedService; - (void)testService; - +- (void)performRemoteAttestationWithSuccess:(void (^)(RemoteAttestation *_Nonnull remoteAttestation))successHandler + failure:(void (^)(NSError *_Nonnull error))failureHandler; @end NS_ASSUME_NONNULL_END diff --git a/SignalServiceKit/src/Contacts/ContactDiscoveryService.m b/SignalServiceKit/src/Contacts/ContactDiscoveryService.m index 2acf7e5d5..f034b680b 100644 --- a/SignalServiceKit/src/Contacts/ContactDiscoveryService.m +++ b/SignalServiceKit/src/Contacts/ContactDiscoveryService.m @@ -8,6 +8,7 @@ #import "Cryptography.h" #import "NSData+OWS.h" #import "NSDate+OWS.h" +#import "OWSError.h" #import "OWSRequestFactory.h" #import "TSNetworkManager.h" #import @@ -219,34 +220,48 @@ NS_ASSUME_NONNULL_BEGIN - (void)testService { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ - [self performRemoteAttestation]; + [self + performRemoteAttestationWithSuccess:^(RemoteAttestation *_Nonnull remoteAttestation) { + DDLogDebug(@"%@ in %s succeeded", self.logTag, __PRETTY_FUNCTION__); + } + failure:^(NSError *_Nonnull error) { + DDLogDebug(@"%@ in %s failed with error: %@", self.logTag, __PRETTY_FUNCTION__, error); + }]; }); } -- (void)performRemoteAttestation +- (void)performRemoteAttestationWithSuccess:(void (^)(RemoteAttestation *_Nonnull remoteAttestation))successHandler + failure:(void (^)(NSError *_Nonnull error))failureHandler { - [self performRemoteAttestationAuth]; + [self + getRemoteAttestationAuthWithSuccess:^(RemoteAttestationAuth *_Nonnull auth) { + [self performRemoteAttestationWithAuth:auth success:successHandler failure:failureHandler]; + } + failure:failureHandler]; } -// TODO: Add success and failure? -- (void)performRemoteAttestationAuth +- (void)getRemoteAttestationAuthWithSuccess:(void (^)(RemoteAttestationAuth *))successHandler + failure:(void (^)(NSError *_Nonnull error))failureHandler { TSRequest *request = [OWSRequestFactory remoteAttestationAuthRequest]; [[TSNetworkManager sharedManager] makeRequest:request success:^(NSURLSessionDataTask *task, id responseDict) { - dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ RemoteAttestationAuth *_Nullable auth = [self parseAuthToken:responseDict]; if (!auth) { DDLogError(@"%@ remote attestation auth could not be parsed: %@", self.logTag, responseDict); + NSError *error = OWSErrorMakeUnableToProcessServerResponseError(); + failureHandler(error); return; } - [self performRemoteAttestationWithAuth:auth]; + + successHandler(auth); }); } failure:^(NSURLSessionDataTask *task, NSError *error) { NSHTTPURLResponse *response = (NSHTTPURLResponse *)task.response; DDLogVerbose(@"%@ remote attestation auth failure: %zd", self.logTag, response.statusCode); + failureHandler(error); }]; } @@ -276,6 +291,8 @@ NS_ASSUME_NONNULL_BEGIN } - (void)performRemoteAttestationWithAuth:(RemoteAttestationAuth *)auth + success:(void (^)(RemoteAttestation *_Nonnull remoteAttestation))successHandler + failure:(void (^)(NSError *_Nonnull error))failureHandler { ECKeyPair *keyPair = [Curve25519 generateKeyPair]; @@ -290,15 +307,24 @@ NS_ASSUME_NONNULL_BEGIN success:^(NSURLSessionDataTask *task, id responseJson) { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ // TODO: Handle result. - [self parseAttestationResponseJson:responseJson - response:task.response - keyPair:keyPair - enclaveId:enclaveId]; + RemoteAttestation *_Nullable attestation = [self parseAttestationResponseJson:responseJson + response:task.response + keyPair:keyPair + enclaveId:enclaveId]; + + if (!attestation) { + NSError *error = OWSErrorMakeUnableToProcessServerResponseError(); + failureHandler(error); + return; + } + + successHandler(attestation); }); } failure:^(NSURLSessionDataTask *task, NSError *error) { NSHTTPURLResponse *response = (NSHTTPURLResponse *)task.response; DDLogVerbose(@"%@ remote attestation failure: %zd", self.logTag, response.statusCode); + failureHandler(error); }]; }