Skip to content

Commit

Permalink
Use forwardingTargetForSelector: to simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
nabla-c0d3 committed Dec 19, 2012
1 parent 8810a54 commit b6bc3de
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 93 deletions.
24 changes: 3 additions & 21 deletions HookedNSURLConnectionDelegate.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@



@interface HookedNSURLConnectionDelegate : NSObject <NSURLConnectionDelegate> {

id originalDelegate; // The NSURLConnectionDelegate we're going to proxy
Expand All @@ -22,20 +20,11 @@
// Mirror the original delegate's list of implemented methods
- (BOOL)respondsToSelector:(SEL)aSelector ;


// NSURLConnectionDelegate - Required methods
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data ;
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response ;
- (void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite ;
- (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse ;
- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse ;
- (void)connectionDidFinishLoading:(NSURLConnection *)connection ;
// Forward messages to the original delegate if the proxy doesn't implement the method
- (id)forwardingTargetForSelector:(SEL)sel;


// NSURLConnectionDelegate - Optional methods
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error ;
- (NSInputStream *)connection:(NSURLConnection *)connection needNewBodyStream:(NSURLRequest *)request NS_AVAILABLE(10_6, 3_0); ;

// Methods implemented by the proxy

// NSURLConnectionDelegate - Custom cert validation - Strategy #1
- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;
Expand All @@ -50,11 +39,4 @@
- (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection;


// NSURLAuthenticationChallengeSender - so we can intercept the App's response to challenge.sender
- (void)cancelAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;
- (void)continueWithoutCredentialForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;
- (void)useCredential:(NSURLCredential *)credential forAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;
- (void)performDefaultHandlingForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;
- (void)rejectProtectionSpaceAndContinueWithChallenge:(NSURLAuthenticationChallenge *)challenge;

@end
76 changes: 5 additions & 71 deletions HookedNSURLConnectionDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ +(BOOL)shouldHookNSURLConnectionFromPreference:(NSString*) preferenceFilePath {
else {
id shouldHook = [plist objectForKey:@"killSwitchNSURLConnection"];
if (shouldHook) {
[plist release];
return [shouldHook boolValue];
}
else { // Property was not set, don't hook
NSLog(@"SSL Kill Switch - Preference not set.");
[plist release];
return FALSE;
}
[plist release];
}
}

Expand Down Expand Up @@ -79,39 +80,10 @@ - (BOOL)respondsToSelector:(SEL)aSelector {
}


// NSURLConnectionDelegate - Required methods: Just forward the call to the original delegate
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
return [originalDelegate connection:connection didReceiveData:data];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
return [originalDelegate connection:connection didReceiveResponse:response];
}

- (void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite {
return [originalDelegate connection:connection didSendBodyData:bytesWritten totalBytesWritten:totalBytesWritten totalBytesExpectedToWrite: totalBytesExpectedToWrite];
}

- (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse {
return [originalDelegate connection:connection willCacheResponse:cachedResponse];
}

- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse {
return [originalDelegate connection:connection willSendRequest:request redirectResponse:redirectResponse];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
return [originalDelegate connectionDidFinishLoading:connection];
}


// NSURLConnectionDelegate - Optional methods: Just forward the call to the original delegate
- (NSInputStream *)connection:(NSURLConnection *)connection needNewBodyStream:(NSURLRequest *)request NS_AVAILABLE(10_6, 3_0) {
return [originalDelegate connection:connection needNewBodyStream:request NS_AVAILABLE(10_6, 3_0)];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
[originalDelegate connection:connection didFailWithError:error];
// Forward messages to the original delegate if the proxy doesn't implement the method
- (id)forwardingTargetForSelector:(SEL)sel {
return originalDelegate;
}


Expand All @@ -120,14 +92,6 @@ - (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticatio

if([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])
{
// Call the original delegate method in case it changes the application's state but intercept the response
// Not sure how to make the App's validation method succeed
/*if (customCertValidationMethod1) { // The App implements this method
id senderProxy = self;
NSURLAuthenticationChallenge* challengeProxy = [[NSURLAuthenticationChallenge alloc] initWithAuthenticationChallenge:challenge sender:senderProxy];
[originalDelegate connection:connection willSendRequestForAuthenticationChallenge:challengeProxy];
}*/

// Now accept the certificate and send the response to the real challenge.sender
[challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]
forAuthenticationChallenge:challenge];
Expand All @@ -151,14 +115,6 @@ - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallen

if([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])
{
// Call the original delegate method in case it changes the application's state but intercept the response
// Not sure how to make the App's validation method succeed
/*if (customCertValidationMethod2) { // The App implements this method
id senderProxy = self;
NSURLAuthenticationChallenge* challengeProxy = [[NSURLAuthenticationChallenge alloc] initWithAuthenticationChallenge:challenge sender:senderProxy];
[originalDelegate connection:connection didReceiveAuthenticationChallenge:challengeProxy];
}*/

// Now accept the certificate and send the response to the real challenge.sender
[challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]
forAuthenticationChallenge:challenge];
Expand All @@ -173,26 +129,4 @@ - (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection {
}


// NSURLAuthenticationChallengeSender - so we can intercept the App's response to challenge.sender
- (void)cancelAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
NSLog(@"SSL Kill Switch - Intercepted cancelAuthenticationChallenge");
}

- (void)continueWithoutCredentialForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
NSLog(@"SSL Kill Switch - Intercepted continueWithoutCredentialForAuthenticationChallenge");
}

- (void)useCredential:(NSURLCredential *)credential forAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
NSLog(@"SSL Kill Switch - Intercepted useCredential:forAuthenticationChallenge");
}

- (void)performDefaultHandlingForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
NSLog(@"SSL Kill Switch - Intercepted performDefaultHandlingForAuthenticationChallenge");
}

- (void)rejectProtectionSpaceAndContinueWithChallenge:(NSURLAuthenticationChallenge *)challenge {
NSLog(@"SSL Kill Switch - Intercepted rejectProtectionSpaceAndContinueWithChallenge");
}


@end
2 changes: 1 addition & 1 deletion Tweak.xm
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

+ (NSURLConnection *)connectionWithRequest:(NSURLRequest *)request delegate:(id < NSURLConnectionDelegate >)delegate {

id hookedResult;
NSURLConnection *hookedResult;
HookedNSURLConnectionDelegate* delegateProxy = [[HookedNSURLConnectionDelegate alloc] initWithOriginalDelegate: delegate];
hookedResult = %orig(request, delegateProxy);
[delegateProxy release]; // NSURLConnection retains the delegate
Expand Down

0 comments on commit b6bc3de

Please sign in to comment.