Skip to content

Instantly share code, notes, and snippets.

@pt-arvind
Created July 19, 2013 18:50
Show Gist options
  • Save pt-arvind/6041458 to your computer and use it in GitHub Desktop.
Save pt-arvind/6041458 to your computer and use it in GitHub Desktop.
public key pinning
- (void)connection:(NSURLConnection *)connection
willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
SecTrustRef serverTrust = challenge.protectionSpace.serverTrust;
BOOL shouldPerformConnectionSecurityMeasures = [[MINetworkSecurityManager sharedManager] shouldPerformConnectionSecurityMeasuresForURLConnection:connection];
BOOL trusted = shouldPerformConnectionSecurityMeasures ? [[MINetworkSecurityManager sharedManager] evaluateServerTrust:serverTrust] : [[MINetworkSecurityManager sharedManager] evaluateTrust:serverTrust forSecurityModel:MINetworkSecurityModelStandard error:nil];
if (trusted) {
NSURLCredential *credential = [NSURLCredential credentialForTrust:serverTrust];
[[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
} else {
self.networkSecurityErrorDetected = YES;
[[challenge sender] cancelAuthenticationChallenge:challenge];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment