I am using iOS 10. I am evaluating a self signed certificate as below
-(void) connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
NSURLProtectionSpace *protectionSpace = [challenge protectionSpace];
if ([protectionSpace authenticationMethod] == NSURLAuthenticationMethodServerTrust) {
SecTrustRef trust = [protectionSpace serverTrust];
SecPolicyRef policyOverride = SecPolicyCreateSSL(true, (CFStringRef)@"HOSTNAME");
SecTrustSetPolicies(trust, policyOverride);
CFMutableArrayRef certificates = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks);
/* Copy the certificates from the original trust object */
CFIndex count = SecTrustGetCertificateCount(trust);
CFIndex i=0;
for (i = 0; i < count; i++) {
SecCertificateRef item = SecTrustGetCertificateAtIndex(trust, i);
CFArrayAppendValue(certificates, item);
}
/* Create a new trust object */
SecTrustRef newtrust = NULL;
if (SecTrustCreateWithCertificates(certificates, policyOverride, &newtrust) != errSecSuccess) {
/* Probably a good spot to log something. */
NSLog(@"Error in SecTrustCreateWithCertificates");
[connection cancel];
return;
}
CFRelease(policyOverride);
/* Re-evaluate the trust policy. */
SecTrustResultType secresult = kSecTrustResultInvalid;
if (SecTrustEvaluate(trust, &secresult) != errSecSuccess) {
/* Trust evaluation failed. */
[connection cancel];
// Perform other cleanup here, as needed.
return;
}
switch (secresult) {
//case kSecTrustResultInvalid:
//case kSecTrustResultRecoverableTrustFailure:
case kSecTrustResultUnspecified: // The OS trusts this certificate implicitly.
case kSecTrustResultProceed: // The user explicitly told the OS to trust it.
{
NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
[challenge.sender useCredential:credential forAuthenticationChallenge:challenge];
return;
}
default: ;
/* It's somebody else's key. Fall through. */
[challenge.sender performDefaultHandlingForAuthenticationChallenge:challenge];
break;
}
/* The server sent a key other than the trusted key. */
[connection cancel];
// Perform other cleanup here, as needed.
}
}
The result after evaluating is 'kSecTrustResultUnspecified' and again the same method 'willSendRequestForAuthenticationChallenge' is being called recursively. Not sure why the method is being called recursively. Let me know any issue with the code.
Thanks