I have been trying to implement a way to do public key pinning in my iOS 8+ app using NSURLSession.
What I do is just implement the delegate method didReceiveChallenge and get the challenge.protectionSpace.authenticationMethod and check if it is equal to NSURLAuthenticationMethodServerTrust.
If it is equal to NSURLAuthenticationMethodServerTrust i check the certificate and compare it with my local copy.
This works fine on iOS 8 but on iOS 9 I don't receive an authentication method equal to NSURLAuthenticationMethodServerTrust. I receive NSURLAuthenticationMethodClientCertificate so I can not access the challenge.protectionSpace.serverTrust property.
Any ideas?
public func URLSession(session: NSURLSession, didReceiveChallenge challenge: NSURLAuthenticationChallenge, completionHandler: (NSURLSessionAuthChallengeDisposition, NSURLCredential!) -> Void)
{
if(challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust)
{
// Verify the identity of the server
println("Trusted")
return challenge.sender.performDefaultHandlingForAuthenticationChallenge!(challenge)
}
println("Not trusted")
return challenge.sender.cancelAuthenticationChallenge(challenge)
}
}